2. .eq()
.eq( index )
indexAn integer indicating the 0-based position of the element.
.eq( -index )
-indexAn integer indicating the position of the element, counting backwards from the last element in the set.
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
$('li').eq(2).css('background-color', 'red');
The result: red background for item 3.
Note that the supplied index is zero-based, and refers to the position of the element within the jQuery object, not
within the DOM tree.
3. .addClass(), .removeClass()
● Dynamically add class to an element
$("p").addClass("myClass");
● Dynamically add multiple classes at a time to an element
$("p").addClass("myClass yourClass");
● Dynamically remove class from an element
$("p").removeClass("myClass");
● Dynamically remove multiple classes at a time from an element
$("p").removeClass("myClass yourClass");
● In overriding - the only thing that matters is the order of the css definitions
in the style sheet
4. .hasClass()
.hasClass( className )
className The class name to search for.
<div id="mydiv" class="foo bar"></div>
$('#mydiv').hasClass('foo') - return true
$('#mydiv').hasClass('bar') - return true
$('#mydiv').hasClass('quux') - return false
5. .toggleClass()
.toggleClass( className )
className One or more class names (separated by spaces) to be toggled for each element in the matched set.
.toggleClass( className, switch )
className One or more class names (separated by spaces) to be toggled for each element in the matched set.
switch A Boolean (not just truthy/falsy) value to determine whether the class should be added or removed.
We have:
<div class="tumble">Some text.</div>
We do : $('div.tumble').toggleClass('bounce')
and get:
<div class="tumble bounce">Some text.</div>
We do again : $('div.tumble').toggleClass('bounce')
and get:
<div class="tumble">Some text.</div>
Applying .toggleClass('bounce spin') to the same <div> alternates between <div class="tumble bounce spin"> and <div class="
tumble">.