<input id="NameAjax" class="ac_input" type="text" value="">
And using jquery:
).click(function(e) {
document.getElementById("NameAjax").value = 1;
}
But after the click the value does not change:
<input id="NameAjax" class="ac_input" type="text" value="">
I am looking for the output to look exactly like:
<input id="NameAjax" class="ac_input" type="text" value="1">
How to fix it ?
4 Answers 4
$("#elementID").on('click', function() {
$("#NameAjax").val('1');
});
answered Jan 18, 2012 at 4:12
adeneo
319k29 gold badges410 silver badges392 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You mentioned Jquery so I am going to assume you are using it. If so try this:
$('#NameAjax').attr('value','1')
The first part $('#NameAjax') selects the input and the second attr('value','1') sets the value attribute to 1
Andrew Whitaker
126k32 gold badges296 silver badges308 bronze badges
answered Jan 18, 2012 at 4:10
Dan Walmsley
2,8318 gold badges28 silver badges46 bronze badges
3 Comments
James Montagne
You should use
val not attr.Andrew Whitaker
or just
$("#NameAjax").val(1)Dan Walmsley
new to Jquery didn't know about the .val() method. Good to know thanks.
Use the val method:
$('#NameAjax').val('1');
Don't use jquery only half the way. And don't use attr function to set a value.
answered Jan 18, 2012 at 4:12
gdoron
151k59 gold badges302 silver badges376 bronze badges
Comments
$("element_idOrclass").click(function() {
$("#NameAjax").attr("value","1");
}
answered Jan 18, 2012 at 4:14
Tasnim Reza
6,0603 gold badges28 silver badges31 bronze badges
1 Comment
Tasnim Reza
@gdorn sorry ! after post the answer i see already 3 correct answer !! :)
lang-js
attrto set value.