← Back to jQuery Course | Chapter 16: jQuery vs Modern JS & Interview Prep | Lesson 1 of 6

jQuery Selectors के Native DOM Alternatives

Modern JavaScript getElementById या querySelector से elements select कर सकता है। ये methods jQuery के बिना काम करते हैं।
Syntax
javascript
document.querySelector(selector);
document.querySelectorAll(selector);

ID से Select करना

document.getElementById() और document.querySelector() किसी jQuery ID या CSS selector के native browser equivalents हैं, और इन्हें ज़रूरत नहीं।

उदाहरण: Select by ID

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="text">Text</p>
    <script>
      console.log($("#text").text());
      console.log(document.getElementById("text").textContent);
    </script>
  </body>
</html>

Class से Select करना

jQuery एक CSS class selector वाले उसी $() function से class के हिसाब से elements select करता है, जैसे $('.item')। Vanilla JavaScript same result।

उदाहरण: Select by Class

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p class="item">One</p>
    <script>
      console.log($(".item").length);
      console.log(document.querySelectorAll(".item").length);
    </script>
  </body>
</html>

querySelector और querySelectorAll

querySelector() किसी दिए गए CSS selector से match करने वाला सिर्फ पहला element return करता है, जबकि querySelectorAll() हर matching element को एक NodeList।

उदाहरण: querySelector and querySelectorAll

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p class="item">One</p><p class="item">Two</p>
    <script>
      console.log(document.querySelector(".item").textContent);
      console.log(document.querySelectorAll(".item").length);
    </script>
  </body>
</html>

Selector Comparison

jQuery selectors concise हैं और jQuery objects के साथ automatically attached convenient built-in methods का एक बड़ा set आता है। Native selectors उसे हटा देते हैं।

उदाहरण: Selector Comparison

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p class="item">Text</p>
    <script>
      $(".item").css("color", "red");
      document.querySelectorAll(".item").forEach(function(el) { el.style.color = "red"; });
    </script>
  </body>
</html>

एक छोटा Selector Project

दोनों में चुनना अक्सर इस बात पर depend करता है कि project का बाकी हिस्सा पहले से क्या इस्तेमाल करता है: native selectors हर modern browser में सीधे built होते हैं।

उदाहरण: Small Selector Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="text">Text</p>
    <script>
      console.log($("#text").text() === document.getElementById("text").textContent);
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. document.querySelector(".item") इस्तेमाल करना और $(".item") जैसे सभी matches की उम्मीद करना, जबकि यह सिर्फ पहला return करता है; querySelectorAll इस्तेमाल करें।
  2. एक querySelectorAll() result को jQuery object की तरह treat करना और उस पर .hide() call करना, जबकि यह एक NodeList है जिसे loop चाहिए।
  3. # के साथ document.getElementById("#text") लिखना, जबकि getElementById सिर्फ id लेता है: "text"।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.