JORd JORd - 4 months ago 9
jQuery Question

input value doesnt be updated with jquery

I want to change, the value of an input in jquery but it dosent work.
Here is my code,

<script src="https://code.jquery.com/jquery-1.10.2.js">
$('#two').click(function(){
$('#count').val('2');
});

$('#four').click(function(){
$('#count').val('4');
});
</script>

<input type="button" id ='two' value="2Q">
<input type="button" id ='four' value="4Qy">

Total <input type="text" id="count" value="">

Answer

You need to wait for your document to be ready before attaching listeners to the input fields.

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function() {
  $('#two').on('click', function() {
    $('#count').val('2');
  });
});
</script>

<input type="button" id ='two' value="2Q">
<input  type="button" id ='four' value="4Qy">

Total <input type="text" id="count" value="">