Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 1.17 KB

no-class-state.md

File metadata and controls

38 lines (31 loc) · 1.17 KB

no-class-state

Disallows .hasClass and .toggleClass to discourage querying the DOM for state information. .toggleClass may be used with a boolean argument as then it behaves like .addClass/.removeClass.

Rule details

❌ Examples of incorrect code:

$( 'div' ).hasClass();
$div.hasClass();
$( 'div' ).first().hasClass();
$( 'div' ).append( $( 'input' ).hasClass() );
$( 'div' ).toggleClass( 'myClass' );
$div.toggleClass( 'myClass' );
$( 'div' ).first().toggleClass( 'myClass' );
$( 'div' ).append( $( 'input' ).toggleClass( 'myClass' ) );

✔️ Examples of correct code:

hasClass();
[].hasClass();
div.hasClass();
div.hasClass;
toggleClass();
[].toggleClass();
div.toggleClass();
div.toggleClass;
$div.toggleClass( 'myClass', true );
$div.toggleClass( 'myClass', false );

Resources