jQuery addClass Method
jQuery addClass Method : is used to add class(es) to the selected elements.
jQuery addClass Method Syntax
$(selector).addClass(classname,function(index,currentClassName));
index: (Interger) Index of the current element in the current set.
currentClassName(String)Current Class name in the matching set.
jQuery addClass() Method Example 1
<script type="text/javascript"> $(document).ready(function(){ $( "#addClassExample" ).on( "click", function(event) { $("#addClass").addClass('myclass'); }); }); </script> |
jQuery addClass() Method Example 2
<script type="text/javascript"> $(document).ready(function(){ $( "#addClassExample" ).on( "click", function(event) { $("ul li").addClass(function(index,currentClass){ var addclass; if(currentClass == 'myClass'){ addclass = 'yellow-bg'; } return addclass; }); }); }); </script> |
Output
Advertisements