Add/remove classes to list items
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/* w ww . j a v a 2s.c o m*/ <script> $(document).ready(function(){ $("button").click(function(){ $("li").toggleClass(function(n){ return "listitem_" + n; }); }); }); </script> <style> .listitem_1, .listitem_3 { color: red; } .listitem_0, .listitem_2 { color: blue; } </style> </head> <body> <h1>This is a heading</h1> <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> </ul> <button>test</button> </body> </html>