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();
Related
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>
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.
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';
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.
This question already has answers here:
Finding the 'type' of an input element
(4 answers)
Closed 9 years ago.
Please disregard this. There was an error in my script and I was looking on here for 15 minutes.
How do you find the type of an input field only using Javascript?
Per say you have these input field:
<input type="checkbox" name="confirm" id="confirm" value="Y"/>
<input type="text" name="firstName" id="firstName" />
How do you find the those field's input type?
It's as simple as:
var type = document.getElementById("confirm").type
Plain Javascript:
var type = document.getElementById('confirm').type;
jQuery:
var type = $('#confirm').attr('type');