Chris
Chris

Reputation: 435

jquery each input hasClass

For my needs I use

$('#form :input').each( function(i) {
    if ( !$(this).hasClass('donot') ) {
        $(this).attr('disabled', 'disabled');
    }
});

is there a better way to not use the if condition to check if the input has the class 'donot' ?

Thanks for your help...

Chris

Upvotes: 8

Views: 16192

Answers (2)

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this and also you don't even need each loop to do this.

$('#form input:not(.donot)').attr('disabled', 'disabled');

Upvotes: 3

Naftali
Naftali

Reputation: 146310

$('#form input:not(.donot)').each( function(i) {
    $(this).attr('disabled', 'disabled');
});

And there you go :-D

Docs for :not() selector


Or you can also do:

$('#form input').not('.donot').each( function(i) {
    $(this).attr('disabled', 'disabled');
});

Docs for .not()

Upvotes: 9

Related Questions