Tsea Tsea - 7 months ago 11
Javascript Question

Why is Jquery click function requiring 2 clicks

I'm clicking a button to fill a hidden value to use with php. This jQuery is making me click the button twice before it enters the value into the hidden field how do I make it only one click?

jquery

$(document).ready(function() {

$('.btn-group button').click(function(){

$("#hidd").val( $(".btn-group").find('.active').val());

});

});


html

<div class="btn-group">
<button type="button" class="btn btn-default" value="150">150</button>
<button type="button" class="btn btn-default" value="450">450</button>
</div>
<input type="hidden" id="hidd" name="pass_amount" value=""></input>

Answer

You should change this line :

$("#hidd").val( $(".btn-group").find('.active').val());

by this one :

$("#hidd").val( $(this).val());

Because the first click make the button active , then the second one retrieves the value of the previous activated one.