jQuery removeClass() with callback function

Introduction

Remove class listitem from the li elements with index 0 and 1

View in separate window

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

<h1>This is a heading</h1>

<ul>
  <li class="listitem">CSS</li>
  <li class="listitem">HTML</li>
  <li class="listitem">Java</li>
  <li class="listitem">Javascript</li>
</ul>

<button>test</button>

</body>
</html>



PreviousNext

Related