55

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?

7 Answers 7

69
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 )
Sign up to request clarification or add additional context in comments.

2 Comments

You might also filter that list by the name attribute this group of radio buttons share.
if ( !($("input").is(':checked')) ) I'll add extra parenthesis too to make it better.
26
if ($("input").is(":not(:checked)"))

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

Comments

10

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
}

Comments

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

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

Comments

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

this will also work

input:radio:not(:checked)

or

:radio:not(:checked)

2 Comments

This will work for sure. Took me a while to figure out. It's the most elegant you can get without writing any code.
Works great for me as selector as well.
0
(!$('#radio').is(':checked'))

This worked for me

Comments

-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.