Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of an option button in jQuery mobile

If I have the following jQuery mobile html;

<fieldset data-role="controlgroup" data-type="horizontal" name="Smoker" id="Smoker">
  <input name="radio3" id="radio3" value="Yes" type="radio" />
  <label for="radio3">Yes</label>
  <input name="radiobuttons1" id="radio4" value="No" type="radio" />
  <label for="radio4">No</label>
</fieldset>

I thought I could do this in jQuery;

$("#Smoker").val()

However this yields no result. Do I in fact need to query both the inputs to see which one has been checked or is there a nice jQuery Mobile way to do this?

like image 487
griegs Avatar asked May 16 '12 04:05

griegs


2 Answers

try

$("#Smoker :radio:checked").val();
like image 170
Rafay Avatar answered Nov 19 '22 20:11

Rafay


First your radio buttons should have the same name attribute, otherwise selecting one doesn't deselct the other, so instead of your current markup you should have something like the following

<fieldset data-role="controlgroup" data-type="horizontal" name="Smoker" id="Smoker">
  <input name="smokerRdo" id="radio3" value="Yes" type="radio" />
  <label for="radio3">Yes</label>
  <input name="smokerRdo" id="radio4" value="No" type="radio" />
  <label for="radio4">No</label>
</fieldset>

AS for getting the value it's the same as regular jQuery, however your trying to get the value of the fieldset, not the radio group, instead try

 $('input[name=smokerRdo]:checked').val()
like image 33
Jack Avatar answered Nov 19 '22 18:11

Jack