Updating value of input html using javascript fails [duplicate] - javascript

This question already has answers here:
HTML Input is not updating value attribute on change
(3 answers)
Modifying "value" attribute of "text" input not working using JavaScript
(1 answer)
Closed 3 years ago.
I have a simple html:
<td><input type="text" style="border:none;" size="12" id="titleId" name="title" value="333"></td>
and I use the code below to update the value of the input:
document.getElementById('titleId').value = "999";
However, when I click "inspect element" the value is still "333", but on the page it shows "999".
I need to update the value to "999" for pdf printing purposes.
How can I update the values using js?

In addition to #Phil's Comment, this is how you can change attribute's value via javascript
function test(){
document.getElementById("in").setAttribute("value", "999");
}
<input id="in" value="333"/>
<button onclick="test()">Click Me</button>

Related

Why is this returning undefined instead of the value? [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 4 years ago.
I have an input for in my HTML code, and I want to capture the input as a variable in javascript.
HTML:
<form id="answer">
Answer: <input type="text" answer="response"><br>
<input type="button" value="Submit" onclick="answerInput()"> <br><br>
</form>
Javascript:
var answer = document.getElementById('answer').value;
I would expect this to return the value of what was put in the field before submit was hit, but it returns undefined. I need someone to be able to put in a value (in this case a number) and have that number be available as a variable.
Thank you for being patient, I'm new to all this and teaching myself.
Put the id answer on the input tag not on the form tag
function answerInput()
{
var answer = document.getElementById('answer').value;
console.log(answer)}
<form>
Answer: <input type="text" id="answer" answer="response"><br>
<input type="button" value="Submit" onclick="answerInput()"> <br><br>
</form>
Your form has the id of "answer". Your input has no id.
Javascript checks the element with the id of "answer" for its "value" property, but the form does not have a value property, so it returns undefined.
So you can just move id="answer" from the form tag to the input tag to get your desired result.

How to set a value to a hidden type input from javascript [duplicate]

This question already has answers here:
How to set the value of a input hidden field through JavaScript?
(5 answers)
Closed 5 years ago.
I am working with jsp. I want to set a value from JavaScript to HTML hidden type input in order to pass the value to another page.
This is my HTML code:
<form id="reg" method="post" action="/questionnaire.jsp">
<input type="hidden" name="userId" value="123">
<button id="submit" name="sr-buttobmitun">Submit</button>
</form>
JavaScript is like:
<script>
var userId = "123";
</script>
If you give the hidden input an id, you can then query the DOM for that element and set the value, like so:
document.getElementById('userid').value = '123';

Which attribute will stop users changing input data but allow jQuery to modify? [duplicate]

This question already has answers here:
What's the difference between disabled="disabled" and readonly="readonly" for HTML form input fields?
(7 answers)
Closed 7 years ago.
I have seen people recommending
<input name="input" readonly="readonly">
and
<input name="input" disabled="disabled">
But which of these two will:
Stop users changing the value
Allow jQuery to update the value
Use readonly, because disabled will also prevent the field from being submitted.

Can I save the value of a textbox? [duplicate]

This question already has answers here:
Get the value in an input text box
(13 answers)
Closed 8 years ago.
I have one text box and have written "ABC" in the text box, but I want to obtain the text using jQuery
<input type=text value="ABC">
But when I use jQuery's .html() it returns null
Here's a FIDDLE
<input type="text">
<span></span>
$('input').keyup(function() {
$('span').text($(this).val());
});
or
$('input').keyup(function() {
$('span').html($(this).val());
});

How can I add a value to an attribute using jQuery? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to change the value of a custom attribute
I have input field as such:
<input type="text" size="40" locationcode="" userid="" id="inputQuery">
How can I select the element using its id and and put value onto userid attribute?
You should use a data- prefix to add a custom attribute, i.e.
data-userid="yourValue"
So in your case you should setup your input like this.
<input type="text" size="40" data-locationcode="" data-userid="" id="inputQuery" />
Then you can use:
$('#inputQuery').attr('data-userid', 'my_value');
You can do it like so:
Set the value:
$('#inputQuery').val('VALUE');
Get the value:
$('#inputQuery').val();

Categories