Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Selecting Elements: jQuery vs Vanilla JS</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <ul> <li class="item">Apples</li> <li class="item">Bananas</li> <li class="item">Cherries</li> </ul> <button id="btn">Count Items (both ways)</button> <div id="out" style="font-weight:bold;margin-top:10px;"></div> <script> $("#btn").on("click", function () { // jQuery var jqItems = $(".item"); // Vanilla JS var jsItems = document.querySelectorAll(".item"); $("#out").html( "jQuery found: " + jqItems.length + " items<br>" + "Vanilla JS found: " + jsItems.length + " items" ); }); </script> </body> </html>
Output