Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Toggle the Classes of the Elements in jQuery</title> <style> p{ padding: 10px; cursor: pointer; font: bold 16px sans-serif; } .highlight{ background: yellow; } </style> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ $("p").click(function(){ $(this).toggleClass("highlight"); }); }); </script> </head> <body> <p>Click on me to toggle highlighting.</p> <p class="highlight">Click on me to toggle highlighting.</p> <p>Click on me to toggle highlighting.</p> </body> </html>