Sunday, August 9, 2015

jQuery code to get the selected radio button (and its value)

jQuery code to get the selected radio button and its value

.val() - it looks like we should not use this for radiobuttons.

$('input:radio[name=rblist]').val() - this was giving the value of the radiobutton that was selected (checked) when the page loaded.
If I changed the selection, and then checked .val(), it was showing the previous value still. 

To get the selected radiobutton, I had to iterate over the list of radio buttons and check each one if it is checked or not.

$('input:radio[name=rblist"]').each(function(){ 
  if ($(this).is(':checked')) { 
    rb_value = $(this).val(); 
    console.log("Selected rb value = " + rb_value); 
  } 
});

No comments:

Post a Comment

Followers

Blog Archive