views:

16

answers:

2

In the callback of a HttpRequest I have, I run this jQuery code:

$("input[value='"+data.notetype+"']").click();

So the radio button with the value of data.notetype is selected. This works fine. But I need to perform an action based on what button is now selected.

If I use the change() method like this

$("input[name='note_type']").change(function () { 
   var clicked = $("input[name='note_type']:checked").val()
   alert(clicked);
}); 

it works fine when I click on the radio buttons, but when the radio button is set by the first line of code above, nothing happens.

If I use the click() method like this

$("input[name='note_type']").change(function () { 
  var clicked = $("input[name='note_type']:checked").val()
  alert(clicked);
});

it works fine when I click on the radio buttons, but when the radio button is set by the first line of code above, the message box shows the value of the radio button that was selected, not the one it is changing to.

Can anyone let me know how to fix this problem? Thanks for reading.

+2  A: 
$("input[name='note_type']").click(function () { 
  var clicked = $(this).val();
  alert(clicked);
});
Reigel
A: 

Here is a solution for this problem, I think this will help you

For example following is your html

<input type="radio" name="some_name" value="1"> 1 <br />
<input type="radio" name="some_name" value="2"> 2 <br />
<input type="radio" name="some_name" value="3"> 3 <br />
<input type="radio" name="some_name" value="4"> 4 <br />

Try following jquery for this html

$('input[type=radio]').bind('click', function() { alert( $(this).val() ); });

I hope this will help

Salman Riaz