Radio buttons not checked in jQuery

Jquery

Jquery Problem Overview


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?

Jquery Solutions


Solution 1 - Jquery

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 )

Solution 2 - Jquery

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

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

Solution 3 - Jquery

If my firebug profiler work fine (and i know how to use it well), this:

$('#communitymode').attr('checked')

is faster than

$('#communitymode').is('checked')

You can try on this page :)

And then you can use it like

if($('#communitymode').attr('checked')===true) { 
// do something
}

Solution 4 - Jquery

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

This is jquery 1.3, mind the ' and " signs!

Solution 5 - Jquery

$('input:radio[checked=false]');

this will also work

input:radio:not(:checked)

or

:radio:not(:checked)

Solution 6 - Jquery

(!$('#radio').is(':checked'))

This worked for me

Solution 7 - Jquery

This works too. It seems shortest working notation: !$('#selector:checked')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkd7iwpView Question on Stackoverflow
Solution 1 - JquerysingpolymaView Answer on Stackoverflow
Solution 2 - JqueryKlemen SlavičView Answer on Stackoverflow
Solution 3 - JqueryIonuț StaicuView Answer on Stackoverflow
Solution 4 - Jqueryyevgeny View Answer on Stackoverflow
Solution 5 - JqueryAntwanView Answer on Stackoverflow
Solution 6 - JqueryhassanrazadevView Answer on Stackoverflow
Solution 7 - JqueryLapenkov VladimirView Answer on Stackoverflow