Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery each input hasClass

Tags:

jquery

each

input

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

like image 350
Chris Avatar asked Dec 06 '11 20:12

Chris


2 Answers

$('#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()

like image 54
Naftali Avatar answered Oct 12 '22 11:10

Naftali


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

$('#form input:not(.donot)').attr('disabled', 'disabled');
like image 43
ShankarSangoli Avatar answered Oct 12 '22 10:10

ShankarSangoli