Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Basic Selectors: #id, .class, tag</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style>.hit{background:#ffeb3b;}</style> </head> <body> <h3 id="title">Title</h3> <p class="msg">Hello</p> <p class="msg">World</p> <div>Plain div</div> <button id="byId">#id</button> <button id="byClass">.class</button> <button id="byTag">tag</button> <script> $("#byId").click(function(){ $("#title").toggleClass("hit"); }); $("#byClass").click(function(){ $(".msg").toggleClass("hit"); }); $("#byTag").click(function(){ $("div").toggleClass("hit"); }); </script> </body> </html>
Output