How to set input value with JS - javascript

I have input :
<input type="text" id="nameProduct">
I want set value :
document.getElementById("nameProduct").value="hello";
How can I do it before page load input ?

You can't do it with JavaScript, the dom element object can only get after it loaded. The right way is to set value attribute for input.
<input type="text" id="nameProduct" value="hello">
A simple hack you can do is hide element initially and show it after value updated using JavaScript.
var ele=document.getElementById("nameProduct");
ele.value="hello";
ele.style.display='block';
<input type="text" id="nameProduct" style="display:none">

Related

How do I make an html text input readonly but be able to update with a script?

I have these inputs that take the values of a from a in my table when I click on a row. I want to make it so that the user cannot change the input themselves but want to bring values into them when a user clicks a table row. I will be passing these inputs in as a form. I know that when the input is like this:
that it will not be updated. Is there any other way to do it with an input. Is there a different type of tag I can use that can be passed through a form?
Rather than a read-only <input>, I'd go with a combination of a display element and a hidden form element. Something like:
<div id="my-display">This is a value</div>
<input id="my-input" name="my-input" type="hidden" />
And in the code update both:
$('#my-display').text(yourValue);
$('#my-input').val(yourValue);
You can style the display to the user however you like and don't have to worry about whether or not it "de-activates" the form input.
If you really want it to be an inactive input, you can use the same approach:
<input class="my-input" type="text" disabled />
<input class="my-input" type="hidden" name="my-input" />
Which may even save you a line of code here, since both can now use .val():
$('.my-input').val(yourValue);
Try disabled keyword as here
<div id="my-display">This is a value</div>
<input id="my-input" name="my-input" type="text" disabled/>
You can change the value by javascript as below:
document.querySelector('#my-input').value = 'the value you want to enter by javascript';

JQuery attribute (empty) value selector, not working

The elements & the code.
HTML
<input value="" name="data[Filter][address]" type="text" />
<input value="" name="data[Filter][client]" type="text" />
<input value="" name="data[Filter][tenant]" type="text" />
<input value="" name="data[Filter][contract_end_date]" type="text" />
Javascript
console.log($("[name*='data\\[Filter\\]'][value!='']").serialize());
The problem: even if they are all empty, they are serialized.
Why?
You're looking at the value attribute. You can filter off of the value property instead:
http://jsfiddle.net/Y2P6w/
var $filledElems = $("[name*='data\\[Filter\\]']").filter(function () {
return $.trim(this.value).length;
});
The point is when the input tag gets inserted to the page, no matter it is in the page load or in your dynamic JavaScript codes, if it has the value attribute your selector query would use it or if you change your input's value using setAttribute in JavaScript or .attr() in jQuery, their value attribute actually gets changed, but if you change it with .value in JavaScript or .val() in jQuery or simply change the value in the page as a textbox, the attribute won't change, so you better not use value attributes in your selectors, because they are not reliable, an instead use $("[name*='data\\[Filter\\]']") and filter it as #JasonP has pointed out.

Getting hidden inputs in HTML (that has period in name) from a form using Javascript

I have the following HTML:
<body>
<form action="/test/interop/InteropServlet" method="post" id="formTester" name="formTester">
<input type="hidden" name="ApiName" value=""/>
<input type="hidden" name="test.userId" value="admin"/>
<input type="hidden" name="test.password" value="admin"/>
<input type="hidden" name="test.progId" value="CustomTester"/>
<input type="hidden" name="InteropApiData" value=""/>
<input type="hidden" name="TemplateData" value=""/>
and I would like to use Javascript to get these hidden values and set them on clicking a button. I have the following Javscript method:
function callAPI(myform) {
saveCookies();
myform.ApiName.value=document.getElementById("traceName").value;
myform.TemplateData.value=document.getElementById("templateXMLText").value;
myform.test.userId.value=document.getElementById("userIDText").value;
myform.test.password.value=document.getElementById("passwordText").value;
myform.action="http://"+document.getElementById("urlText").value + "/test/interop/InteropHttpServlet";
myform.submit();
}
and this works for the hidden inputs that do not have a period in the name (ie test.userId, test.password) as I get the error "Error: TypeError: myform.test is undefined". I am unable to rename these hidden inputs due to the fact I do not maintain the code I am calling out to and the variables must be named this.
Is there any way I can read hidden inputs that have a period in the name from a form?
Another option, preferable in my opinion, is to use querySelector() to get the specific element:
myform.querySelector('input[name="test.userId"]').value="whatever";
DEMO: http://jsfiddle.net/xjG6W/
For visual purposes, in the demo I changed test.userId to be type="text". Type in the second textbox and click the button - it will change the first textbox's value (really, it's a hidden input).
References:
https://developer.mozilla.org/en-US/docs/DOM/Element.querySelector
Use the square bracket notation for accessing elements with a period in the name. For ex:
myform['test.userId'].value
In your case, this would become:
...
myform['test.userId'].value=document.getElementById("userIDText").value;
myform['test.password'].value=document.getElementById("passwordText").value;
...

fill input text

How can I fill a text input with a value? I am trying this:
<input id="curso" maxlength="150" name="curso" size="40" type="text"
value="window.location.href.substring(91))" />
But that does not work.
<input id="curso" maxlength="150" name="curso" size="40" type="text">
<script>
document.getElementById("curso").value =
document.getElementById("curso").defaultValue = window.location.href.substring(91);
</script>
You can't execute JavaScript from within an arbitrary HTML attribute. Only <script> elements and event handlers can contain JavaScript, so add a <script> that sets the value.
You also need to set the defaultValue property so that any form reset resets the value to the desired value.

Retrieving hidden input value using jQuery

I have a hidden input in the manner below:
<div id="message">
<input id="hiddeninput" type="hidden">
<span>Message with submit button <input type=button id="confirm" value="Submit"></span>
</div>
The hidden input is given a value after a jQuery POST. I need to retrieve the value that is set, and send it in another jQuery POST.
Interestingly, I get this:
<input id="hiddeninput" type="hidden">34345</input>
after fetching the value from the server in the first jQuery post.
Just $("#hiddeninput").val() does not retrieve the value which I want to send.
What is the correct way to do it in my example?
EDIT: In JQuery, This is how I set the value to the hidden field:
$.post("post.php", function(data){
if(data.length > 0){
var resultObj = eval(data)[0];
if(resultObj.SomeNumber >= 0)
{
$("#hidden").html(resultObj.SomeNumber);
}
});
You have to set the value of the hidden field like this, then it should work
<input id="hiddeninput" type="hidden" value="34345" />
The hidden element
<input id="hiddeninput" type="hidden">
does not have the value attribute. It should be like
<input id="hiddeninput" type="hidden" value="someValue">
$("#hiddeninput").html() would retrieve 34345 from the structure as you show although as stated above the value attribute should be used on a hidden field and then val() will work.
Since <input id="hiddeninput" type="hidden">34345</input> is not the "right" way to format the input tag,a s opposed to <input id="hiddeninput" type="hidden" value="34345"/> you need to use $("#hiddeninput").text()
try explicitly adding the value tag to the input elemeent before the post adds it i.e.
<input id="hiddeninput" type="hidden" value="">
If that doeesn't work have a look at this thread:
jquery selector can't read from hidden field

Categories