2
<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
  • What is the click event bound to? Isn't the output correct? Commented Jan 18, 2012 at 4:04
  • Doesn't it have to be a string? Commented Jan 18, 2012 at 4:07
  • 2
    Are you using view source to check if it changed? Commented Jan 18, 2012 at 4:08
  • You better not use attr to set value. Commented Jan 18, 2012 at 4:18

4 Answers 4

4
$("#elementID").on('click', function() {
    $("#NameAjax").val('1');
});
Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

You should use val not attr.
or just $("#NameAjax").val(1)
new to Jquery didn't know about the .val() method. Good to know thanks.
2

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.

Comments

1
$("element_idOrclass").click(function() { 
    $("#NameAjax").attr("value","1");
}

1 Comment

@gdorn sorry ! after post the answer i see already 3 correct answer !! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.