Remove class by index, Remove class "myClass" from the li elements with index 0 and 1 - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:removeClass

Description

Remove class by index, Remove class "myClass" from the li elements with index 0 and 1

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("li").removeClass(function(n) {
           if (n==0||n==1) {//from ww  w .  j  a va  2 s  .  com
              return "myClass"
           } else {
              return "";
           }
        });
    });
});
</script>
<style>
.myClass {
    color:red;
}
</style>
</head>
<body>

<h1>This is a heading</h1>

<ul>
<li class="myClass">Peter</li>
<li class="myClass">Lois</li>
<li class="myClass">Chris</li>
<li class="myClass">Stewie</li>
</ul>

<button>test</button>

</body>
</html>

Related Tutorials