tags:

views:

26

answers:

5

i am trying to get the value of the raido boxes when you click on them. I am using jquery

i have been looking at change. click. i cant get any of them to work.

 <table> 
  <tr> 
<td>Some text 1 </td> 
<td><input type="radio" value="txt1" name="myRadio" id="myRadio" /></td> 
<td>Some text 2 </td> 
<td><input type="radio" value="txt2" name="myRadio" id="myRadio" /></td> 
<td>Some text 3 </td> 
<td><input type="radio" value="txt3" name="myRadio" id="myRadio" /></td> 
  </tr> 
 </table>
+1  A: 

A different id value should be used for each element something you are not doing and using myRadio as id for multiple elements - a reason why your script is not working.

Alternatively, you can use the class or different id values. Once you do that, you can modify your jQuery code accordingly.

Sarfraz
A: 

The change event should work fine. You just need to look at this.value.

$("input").change(function(){
    alert(this.value);
});​

I've made a jsFiddle to show this working in action.

Jonathon
that is not working right.
Gully
What are you expecting? This returns 'txt1', 'txt2', 'txt3' when I click on each radio button.
Jonathon
Im guessing he wants "Some Text 1". A common mistake with radio buttons - this is not part of the radio button (a radio button does not have text, it has a value).
RPM1984
@RPM and @Jonathon, in IE the `change` event is fired when the focus is changed, so it does not work as expected .. it 'lags' ...
Gaby
didn't even notice it was 'change' and not 'click'. :)
RPM1984
Ahh yes - thanks Gaby. Should have realised this as I had the same issue with checkboxes in IE that I'd solved with a focus/blur on `click` event (needed to have the logic in the `change` for value changes in code) :)
Jonathon
+1  A: 

Depends on how you try to get the value from them ...

using the .val() inside the click event works just fine..

example at http://www.jsfiddle.net/aKTcu/

$('input[type=radio]').click(function(){
   alert( $(this).val() );
   // or this.value;
});

but your ID's should be unique regardless, as it is invalid HTML otherwise..

Gaby
A: 

If you have more that on input in your page you can use a different selector:

$('input[name="myRadio"]').click(function(){
    alert(this.value);
});
Oniram
A: 

You can try to ask for the checked attribute, see the example:

<label for="public0"><input type="radio" checked="checked" name="publicar" id="public0" value="TRUE" /> YES</label>

  <label for="public1"><input type="radio" name="publicar" id="public1" value="FALSE" /> NO</label>

And then ask for it:

if ( $("public0").checked == true) 
{ ...} or if ( $("public1").checked == true){...}

You can see the values:

 //alert($("public0").checked);
  //alert($("public1").checked);

Of course you can add more input types=radio, just remember that for xhtml the id for objects should be different.

Nervo Verdezoto