Ashwin
Ashwin

Reputation: 12411

Unable to get value of selected radio

I am trying to get the value of the radio button selected. My html is:

<input type="radio" name="rad" value="one" />One<br />
<input type="radio" name="rad" value="two" />Two<br />
<input type="radio" name="rad" value="three" />Three<br />
<a ​​​​​​​​​​​​​​​​​​​​href="javascript:void(0)" onclick="getSelectedRad()">Click</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

My jquery is:

function getSelectedRad() {
    alert($("input[name='rad']").val());
}

I cannot understand what is wrong happening here?

FIDDLE

Upvotes: 0

Views: 56

Answers (1)

Quentin
Quentin

Reputation: 943209

You are getting the value of the first element that is of type 'input' with a 'name' attribute that has a value of 'rad'.

You want a selector that looks like:

"input[name='rad']:checked"

Upvotes: 4

Related Questions