Tsea Tsea - 1 year ago 61
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?


$(document).ready(function() {

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

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




<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>
<input type="hidden" id="hidd" name="pass_amount" value=""></input>

Answer Source

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.