DooDoo
DooDoo

Reputation: 13467

How to validate Radio Buttons in a Table using jQuery

I have some Radio Buttons that creates in run time and they are inside of a table.How I can check that one of the surly selected using jQuery?

Thanks

Upvotes: 0

Views: 644

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 185973

A general solution:

if ( $( 'input[type="radio"]', '#table' ).is( ':checked' ) ) {
    // validation passes
}

Upvotes: 0

Glory Raj
Glory Raj

Reputation: 17701

you can try like this....

 $('#table tbody tr input[type=radio]').each(function(){
     alert($(this).attr('checked'));
    });

or

There are many ways to do that, e.g., using .each and the .is traversal method:

$("table tr td input[name=something]:radio").each(function() {
    if($(this).is(":checked")) {
        $(this).closest("tr").css("border", "1px solid red");
    } else {
        // do something else
    }
});

or you can do like this .....

define a class on your radio button items, basically your client side HTML should look like <input id="answer-2_1" type="radio" name="answer-2" value="0" class="myrdo" />

now, in your js code, simply wire up an event handler on the class

$(".myrdo").bind("click",function(){if($(this).attr("checked")==true) {alert($(this).val);} });

the above command will simply alert the value of the selected radio button.

Upvotes: 1

Related Questions