How to check which radio button is selected if they're all runat server?

Joe M

I have some code where I need to check radio buttons, and I know how to do it all client side. Here's the markup:

<input type="radio" id="rbInternalUser" name="radioUserType" value="Internal" onclick="setUserType()"/>
<label for="<%=rbInternalUser.ClientID%>">Internal User</label><br />
<input type="radio" id="rbExternalUser" name="radioUserType" value="External" onclick="setUserType()"/>
<label for="<%=rbExternalUser.ClientID%>">External User</label><br />

And the javascript:

<script type="text/javascript">
    function setUserType() {
        var userType = $("input[name=radioUserType]:checked").val();
    }
</script>

But I need these controls set to runat=server because I'm doing some code behind stuff as well:

<input type="radio" id="rbInternalUser" name="radioUserType" value="Internal" onclick="setUserType()" runat="server" />
<label for="<%=rbInternalUser.ClientID%>">Internal User</label><br />
<input type="radio" id="rbExternalUser" name="radioUserType" value="External" onclick="setUserType()" runat="server" />
<label for="<%=rbExternalUser.ClientID%>">External User</label><br />

In that case, how do I find out which button is selected? It doesn't seem like the usual $("#<%=myControl.ClientID%>") will work here. I've tried a few things, and this seems close:

<script type="text/javascript">
    function setUserType() {
        var radioUserType = $("#<%=rbInternalUser.ClientID%>").prop("name");
        var userType = $("input[name=" + radioUserType + "]:checked").val();
    }
</script>

radioUserType is getting the client ID, but then the next line fails, saying, "unrecognized expression: input[name=ctl00$body$EditUser1$radioUserType]:checked".

What do I need to put in place of radioUserType to make this work? Is this possible? How can you get the name of the radio button group and then check which button from the group is selected?

Sachin Singh

While selecting elements using css attribute selector, make sure the value you are passing are enclosed in quotes. Example:

$("input[name='John Doe']")

Notice the single quotes around the name 'John Doe'.

Therefore, you need to change the existing statement

var userType = $("input[name=" + radioUserType + "]:checked").val();

to

var userType = $("input[name='" + radioUserType + "']:checked").val();

which will solve your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to check a radio button with jQuery?

From Java

How can I know which radio button is selected via jQuery?

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

How to know which radio button is selected in jquery?

From Dev

How to check if radio buttons are selected on button click?

From Dev

jQuery Check if a Radio Button is Selected or not

From Dev

how to know which radio button id selected of which row?

From Dev

JQuery Check which radio button was unchecked

From Dev

Check for selected radio button

From Dev

How can I tell which radio button is selected with node.js

From Dev

How to find out which radio button the user selected in Javascript?

From Dev

How to check if all the radio button gropus in a division are checked or not?

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

How to check radio button with radio button value

From Dev

How to check if a radio button is clicked?

From Dev

JavaScript validation to check if a radio button is selected?

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

How to retrieve which radio button within a LI is selected

From Dev

How to create an object which runat server asp

From Dev

how to check which radio button is checked in android?

From Dev

check all radio list radio button has been selected or not on gridview

From Dev

how to know which radio button is selected out of all options?

From Dev

How to style selected radio button

From Dev

How to swap the displayed image based on which radio button is selected?

From Dev

How to disable buttons based on which radio button is selected?

From Dev

How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

From Dev

Is there a way to check if the nth radio button is selected in jQuery?

From Dev

Check if Radio Button is clicked (Selected) - Smarty Template

From Dev

Check if a radio button is selected

Related Related

  1. 1

    How to check a radio button with jQuery?

  2. 2

    How can I know which radio button is selected via jQuery?

  3. 3

    How to check if the radio button is selected or not in Selenium WebDriver?

  4. 4

    How to know which radio button is selected in jquery?

  5. 5

    How to check if radio buttons are selected on button click?

  6. 6

    jQuery Check if a Radio Button is Selected or not

  7. 7

    how to know which radio button id selected of which row?

  8. 8

    JQuery Check which radio button was unchecked

  9. 9

    Check for selected radio button

  10. 10

    How can I tell which radio button is selected with node.js

  11. 11

    How to find out which radio button the user selected in Javascript?

  12. 12

    How to check if all the radio button gropus in a division are checked or not?

  13. 13

    How to check which radio button is selected if they're all runat server?

  14. 14

    How to check radio button with radio button value

  15. 15

    How to check if a radio button is clicked?

  16. 16

    JavaScript validation to check if a radio button is selected?

  17. 17

    How to check if the radio button is selected or not in Selenium WebDriver?

  18. 18

    How to retrieve which radio button within a LI is selected

  19. 19

    How to create an object which runat server asp

  20. 20

    how to check which radio button is checked in android?

  21. 21

    check all radio list radio button has been selected or not on gridview

  22. 22

    how to know which radio button is selected out of all options?

  23. 23

    How to style selected radio button

  24. 24

    How to swap the displayed image based on which radio button is selected?

  25. 25

    How to disable buttons based on which radio button is selected?

  26. 26

    How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

  27. 27

    Is there a way to check if the nth radio button is selected in jQuery?

  28. 28

    Check if Radio Button is clicked (Selected) - Smarty Template

  29. 29

    Check if a radio button is selected

HotTag

Archive