javascript - jQuery .click function executes twice in a row -


i'm working on first program using jquery, i'm having issue. have dialog pop on pageload asks user select date , turn. right now, debugging purposes, have alert every time .click() executes, , reason, seems executes before user clicks , afterward.

there 3 radio buttons, turns 1, 2, , 3. when user clicks turn 1, alert should "1". when user clicks turn 2, alert should "2", etc. reason, alerts previous value new one. searched of code, , there 1 alert, can't figure out calling click() twice. i've tested in ie , chrome , happened both times.

this .click() function:

$("#turn-radio")     .click(function () {     turnvalue = $("input[name='turn-radio']:checked").val();     alert(turnvalue); }); 

if check jsfiddle, you'll see rest of code, make easier figure out problem is.

thanks!

you need change selector: radio button ids different , giving name selector that's why facing problem:

 $("input[name='turn-radio']")         .click(function () {         turnvalue = $("input[name='turn-radio']:checked").val();         alert(turnvalue);     }); 

updated fiddle


Comments