Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether specific radio button is checked

I'm having trouble after looking at the jQuery docs. I'm just trying to return true/false in one my my jquery methods depending on the check of a certain radiobutton and if it's selected or not

I've tried several things but have not been able to get this right:

<input type="radio" runat="server" name="testGroup" id="test1" /><label for="<%=test1.ClientID %>" style="cursor:hand" runat="server">Test1</label> <input type="radio" runat="server" name="testGroup" id="test2" /><label for="<%=test2.ClientID %>" style="cursor:hand" runat="server">Test2</label> <input type="radio" runat="server" name="testGroup" id="test3" /> <label for="<%=test3.ClientID %>" style="cursor:hand">Test3</label> 

and in my method I have this:

return $("input[@name='test2']:checked"); 

I'm getting an undefined on $("input[@name='test2']:checked");

UPDATED:

example:

<input type="radio" runat="server" name="radioGroup"  id="payPalRadioButton" value="paypalSelected" />  

this returns 'undefined' still:

$("input[@name=radioGroup]:checked").attr('payPalRadioButton');  

If I try this, I get 'false' even if I select the radio button:

$('input:radio[name=radioGroup]:checked').val() == 'paypalSelected' 
like image 510
PositiveGuy Avatar asked Feb 03 '10 20:02

PositiveGuy


People also ask

How can I tell if a radio button is checked?

To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.

How do you check if a radio button is checked react?

To check/uncheck a radio button in React, we use the checked property of the input elements, which is set to true when the radio button is checked otherwise it is set to false .


2 Answers

I think you're using the wrong approach. You should set the value attribute of your input elements. Check the docs for .val() for examples of setting and returning the .val() of input elements.

ie.

<input type="radio" runat="server" name="testGroup" value="test2" />  return $('input:radio[name=testGroup]:checked').val() == 'test2'; 
like image 33
ghoppe Avatar answered Sep 29 '22 01:09

ghoppe


Your selector won't select the input field, and if it did it would return a jQuery object. Try this:

$('#test2').is(':checked');  
like image 107
PetersenDidIt Avatar answered Sep 29 '22 02:09

PetersenDidIt