Eamorr
Eamorr

Reputation: 10012

jQuery: can't get radio button value

I have the following jsfiddle:

http://jsfiddle.net/rpp6s/

I want to alert the value of the second radio button form. But if I select a value from the first form, it's value appears in the alert instead of the second form's value.

html:

<form id="formOne">
New invoice <input id="radioNewInvoice" name="groupOne" type="radio" value="new"></input>
 Modify existing invoice<input id="radioModInvoice" name="groupOne" type="radio" value="mod"></input>
</form>



<form id="form2">
    <input id="normal" type="radio" name="groupTwo" value="normal" /> normal <br>
    <input id="30days" type="radio" name="groupTwo" value="30days" /> 30 days <br>
    <input id="60days" type="radio" name="groupTwo" value="60days" /> 60 days <br>
    <input id="90days" type="radio" name="groupTwo" value="90days" /> 90 days <br>
    <input id="90pdays" type="radio" name="groupTwo" value="90+days" /> 90+ days <br>
</form>

<input id="but" type="button" value="click" />

javascript:

$('#but').click(function() {
    alert($("input[@name=groupTwo]:checked").val());
});

Upvotes: 0

Views: 2580

Answers (8)

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17566

 alert($('input[name=groupTwo]:radio:checked').val());

this works fine :D

Upvotes: 1

Sven Bieder
Sven Bieder

Reputation: 5681

Change your alert to

alert($("#form2 input:checked").val()); 

It's the quick and dirty solution, but it works.

Upvotes: 1

Matt Cashatt
Matt Cashatt

Reputation: 24198

$('#but').click(function(){
      alert($("input[name*='groupTwo']:checked").val());
});

Upvotes: 1

Diode
Diode

Reputation: 25135

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

Upvotes: 1

doogle
doogle

Reputation: 3406

$("#but").click(function() {
    alert($("#form2 input:checked").val());
});

Upvotes: 2

kleinohad
kleinohad

Reputation: 5912

use this:

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

or to get a specific form:

alert($('input[name=groupTwo]:checked', '#form2').val());

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270599

Rmove the @ from the input selector.

$('#but').click(function(){
   alert($("input[name=groupTwo]:checked").val()); 
});

Upvotes: 4

STO
STO

Reputation: 10638

$('#but').click(function(){
   alert($("#form2 input[@name=groupTwo]:checked").val()); 
});

Upvotes: 1

Related Questions