rafavinu
rafavinu

Reputation: 305

Get selected radio button value in javascript

I have dynamically created radio buttons and have assigned them ids. Now when i try,

function updateAO(id2) {
    var status = $('input[name=id2]:checked').val();
    alert(status);
}

It prints undefined. Can anyone please tell how to get this value. Thanks in advance!!

Upvotes: 0

Views: 94

Answers (3)

Mad Angle
Mad Angle

Reputation: 2330

To get the selected radio button value use

function updateAO(id2) {
    var status = $('input[name=id2]:checked').val();
    alert(status);
}

To select normal radio button use

function updateAO(id2) {
    var status = $('input[name=id2]').val();
    alert(status);
}

Upvotes: 0

SW4
SW4

Reputation: 71150

The :checked selector will only select inputs currently checked, if the passed input isnt checked at the time, status will remain unset, try changing your JS to:

function updateAO(id2) {
    var status = $('input[name='+id2+']').val();
    alert(status);
}

Upvotes: 1

Etheryte
Etheryte

Reputation: 25310

You're not using your parameter correctly.

var status = $('input[name="' + id2 + '"]:checked').val();

Upvotes: 1

Related Questions