views:

136

answers:

2

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") }

            });
+1  A: 

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();
Nick Craver
+1  A: 

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

James Westgate
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
Nick Craver
@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!)
James Westgate