kobe
kobe

Reputation: 15835

getting undefined while trying to get selected radio value

I have the following code

<input type="radio" class="radioModal" value="Existing" name="SaveToGallery_rdGallery" id="SaveToGallery_rdGallery">

<input type="radio" class="radioModal" value="New" name="SaveToGallery_rdGallery" id="SaveToGallery_rdNewGallery">

When i am trying to get the selected radio value , i am getting undefined

alert($("input[@name='SaveToGallery_rdGallery']:checked").val())

jsfiddle here

http://jsfiddle.net/8F6Uq/

May i know where am i doing wrong

Sometimes i get value like "N"

Upvotes: 0

Views: 2201

Answers (5)

Shef
Shef

Reputation: 45589

1. Check a default value for it to work!

2. Change your selector to this:

alert($("input[name='SaveToGallery_rdGallery']:checked").val())

3. See it live

Upvotes: 1

PeeHaa
PeeHaa

Reputation: 72652

Nothing is checked. So no elements are found.

If an element is checked it will work:

http://jsfiddle.net/8F6Uq/1/

Or add some check:

if ($("input[@name='SaveToGallery_rdGallery']:checked") > 0) {
    // element found
}

Upvotes: 1

locrizak
locrizak

Reputation: 12281

$('input').change(function(){
    alert($("input[@name='SaveToGallery_rdGallery']:checked").val())
});

Because when the page loads there is nothing selected. when you bind the change event and then alert it you can get the value

Upvotes: 1

dxprog
dxprog

Reputation: 138

In your example, none of the radio buttons are checked. To do so you would need to add the checked attribute:

<input type="radio" style="vertical-align:top;margin-right:10px;margin-left:0;" class="radioModal" value="New" name="SaveToGallery_rdGallery" id="SaveToGallery_rdNewGallery" checked="checked">

Of course, in an actual page this would return whatever radio button was actually checked by the user.

Upvotes: 2

ShankarSangoli
ShankarSangoli

Reputation: 69905

Try this

alert($("input[name='SaveToGallery_rdGallery']:checked").val());

Upvotes: 3

Related Questions