Pre-recorded question

Question #124
With jQuery, how do I test whether an element has a particular class?
Answer

The jQuery .hasClass() method checks if at least one of the selected elements have a given class name:

<div id="myId" class="selected"></div>

// true
$('#myId').hasClass('selected')

// false
$('#myId').hasClass('non-existing')

6 events in history
Answer by Alphonsio 06/19/2020 at 04:44:32 PM

The jQuery .hasClass() method checks if at least one of the selected elements have a given class name:

<div id="myId" class="selected"></div>

// true
$('#myId').hasClass('selected')

// false
$('#myId').hasClass('non-existing')

Answer by Alphonsio 06/19/2020 at 04:44:07 PM

The jQuery .hasClass() method checks if at least one of the selected elements have a given class name:

<div id="myId" class="selected"></div>

// true
$('#myId').hasClass('selected')

// false
$('#myId').hasClass('selection')

Answer by Alphonsio 06/19/2020 at 04:41:47 PM

The jQuery .hasClass() method checks if any of the selected elements have a given class name:

<div id="myId" class="selected"></div>

// TRUE
$('#myId').hasClass('selected')

// FALSE
$('#myId').hasClass('selection')

Question by Alphonsio 06/19/2020 at 04:37:53 PM
With jQuery, how do I test whether an element has a particular class?
Answer by Alphonsio 06/19/2020 at 04:37:28 PM

The jQuery .hasClass() method checks if any of the selected elements have a given class name:



Question by Alphonsio 06/19/2020 at 04:36:00 PM
In jQuery, how to check if an element has a given class?
# ID Query URL Count

Icons proudly provided by Friconix.