0

I need to grab the value of the radiobutton option selected with jQuery. I am using the following code but "str" is undefined in Firebug:

 //handling Feed vs. Ingredient RadioButton
            $('[id$=rdoCheck]').change(function() {
                str = $("input[name='rdoCheck']:checked").val();
                //ingredients
                if (str == "ing")
                { $('[id$="lblDescription"]').text("Ingredient") }
                //finished feed
                else if (str == "ffc")
                { $('[id$="lblDescription"]').text("Finished") }

            });
3
  • Can you show the corresponding HTML code? Commented Mar 30, 2010 at 21:02
  • You don't need quotes in the name attribute selector. $("input[name=rdoCheck]:checked") is sufficient.
    – Matt Ball
    Commented Mar 30, 2010 at 21:02
  • Declare your variables with var!!! Geez this happens like every single day here on StackOverflow.
    – Pointy
    Commented Mar 30, 2010 at 21:15

2 Answers 2

1

It looks like you're using ASP.Net. When you look at the rendered html, you'll see it's messing with (prefixing) the name attribute just like it does with IDs.

You'll need to also use an ends-with selector on it, like this:

var str = $("input[name$=rdoCheck]:checked").val();
1

I add classes to my .net generated elements so that I can use simpler jQuery selectors eg

<asp:textbox id="txtName" CssClass="name"/>

$('input.name').text();

Note: You can specify an absolute identifer in asp.net 4.0

2
  • This works for your example, but not so well for radio button groups, for example if you have a user control with a group and multiple copies of it, how would you get a specific group?...gets kinda messy, better off keying off the name...also fixed in 4.0 Commented Mar 30, 2010 at 22:57
  • @Nck - I would specify a different CssClass for each usercontrol instance (unless you are using a repeater or something, then we are potentially in a world of hurt with jQuery!) Commented Mar 30, 2010 at 23:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.