Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio buttons not checked in jQuery

Tags:

jquery

I have this line of code for page load:

if ($("input").is(':checked')) { 

and it works fine when the radio button input is checked. However, I want the opposite. Something along the lines of

if ($("input").not(.is(':checked'))) { 

so that my if statement runs when none of the radiobuttons are selected. What is the best way to go about this?

like image 369
kd7iwp Avatar asked Dec 16 '08 19:12

kd7iwp


People also ask

How can check radio button is checked or not in jQuery?

We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.

How do you check if radio button is checked or not in JavaScript?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.


2 Answers

if ( ! $("input").is(':checked') ) 

Doesn't work?

You might also try iterating over the elements like so:

var iz_checked = true; $('input').each(function(){    iz_checked = iz_checked && $(this).is(':checked'); }); if ( ! iz_checked ) 
like image 90
singpolyma Avatar answered Oct 03 '22 01:10

singpolyma


if ($("input").is(":not(:checked)")) 

AFAIK, this should work, tested against the latest stable jQuery (1.2.6).

like image 23
Klemen Slavič Avatar answered Oct 02 '22 23:10

Klemen Slavič