Four_0h_Three
Four_0h_Three

Reputation: 612

jquery and RadioButton changed event

I have a group of radio buttons all with the same name. I use a selector to attach the .changed event to the radio button grouup by the name. The event fires but the .val() of the radio button group is the value of the group before the change not after the change. How do I get the value after the change, there has to be something I'm doing wrong.

$('[name="Messages"]').click(function () { 
    someFunction('Messages', $('[name="Messages"]').val()) 
});

Upvotes: 1

Views: 1158

Answers (2)

aziz punjani
aziz punjani

Reputation: 25786

You probably want the value for the particular radio button that was clicked on. You can use $(this).val() to get it. Otherwise you need to clarify, there's no such thing as a "group value". Each individual radio button should have it's own value.

$('[name="Messages"]').click(function () { 
    someFunction('Messages', $(this).val());  
});

Upvotes: 4

Mathew Thompson
Mathew Thompson

Reputation: 56449

You're not filtering out unselected radio buttons, therefore your click will always bring back the value of the first radio button in the DOM. Try filtering out on :checked:

$('[name="Messages"]').click(function () { 
    someFunction('Messages',$('[name="Messages"]:checked').val());
});

Fiddle: http://jsfiddle.net/T7Q8T/

Upvotes: 1

Related Questions