user2153384
user2153384

Reputation: 23

Loop in html table and check if a radio is selected

I want a javascript or jquery function that searches in a html table and if no radio is selected notify the user.I only have table id and don't know radio id(s).

Upvotes: 0

Views: 272

Answers (3)

A. Wolff
A. Wolff

Reputation: 74420

You can use is() which returns a boolean:

if (!$('table :radio').is(':checked')) {
        alert('No Radio Button Selected');
    }

If at least one radio button is checked, this won't fired alert()

Upvotes: 0

DEMO

In the code you can replace table with #tableID so that code only runs for that table

$(document).ready(function () {
    if ($('table input[type=radio]:checked').length < 1) {
        alert('No Radio Button Selected');
    }
});

if you want to notify on table click

 $('table').click(function () {
        if ($('table input[type=radio]:checked').length < 1) {
            alert('No Radio Button Selected');
        }
    });

Upvotes: 3

Prash
Prash

Reputation: 1122

$('#table input:radio:checked') will give the radios checked inside table with id `#table`

Upvotes: 0

Related Questions