8

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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

@user1080344 no problem ^_^ happy to help :-D
3

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

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

1 Comment

+1 Ha! that is true :-P I did not even think of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.