← Back to jQuery Course | Chapter 1: Getting Started | Lesson 4 of 6

Document Ready Event

document ready event page structure तैयार होने के बाद चलता है।
Syntax
javascript
$(document).ready(function () {
  // code runs when the DOM is ready
});

$(function () {
  // shorthand
});

Document Ready क्यों मायने रखता है

HTML load होना खत्म होने से पहले jQuery code चलाने का मतलब है कि आप जिन elements को select करने की कोशिश कर रहे हैं वे अभी बस मौजूद ही नहीं होंगे, इसलिए scripts चुपचाप fail हो जाते हैं।

उदाहरण: Why Document Ready Matters

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
      $("#msg").text("This fails silently, #msg does not exist yet");
    </script>
  </head>
  <body>
    <p id="msg">Original</p>
  </body>
</html>

Basic Ready Syntax

$(document).ready(function(){...}) आपके code को तब तक delay करता है जब तक browser पूरे HTML structure को parse नहीं कर लेता, guarantee करते हुए कि callback में आप जिस भी element को reference करें वह असल में DOM में मौजूद है।

उदाहरण: Basic Ready Syntax

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="msg">Original</p>
    <script>
      $(document).ready(function() {
      $("#msg").text("DOM is ready");
      });
    </script>
  </body>
</html>

एक Function के साथ Ready

आप एक single ready callback के अंदर कितने भी jQuery statements रख सकते हैं, और document parse होने पर वे सब एक साथ चलेंगे -- ज़्यादातर beginner jQuery pages अपना पूरा script यहीं रखते हैं।

उदाहरण: Ready with a Function

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="a">A</p><p id="b">B</p>
    <script>
      $(document).ready(function() {
      $("#a").text("Updated A");
      $("#b").text("Updated B");
      });
    </script>
  </body>
</html>

Ready और Button Events

document ready के अंदर click और दूसरे event handlers attach करना ज़रूरी है, क्योंकि अभी तक मौजूद न हुए किसी element पर एक handler bind करना चुपचाप कुछ नहीं करता -- handler बस कभी attach ही नहीं होता।

उदाहरण: Ready and Button Events

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      $(document).ready(function() {
      $("#btn").click(function() {
      console.log("Handler attached inside ready, works correctly");
      });
      });
    </script>
  </body>
</html>

Modern Shorthand

shorthand $(function(){...}) functionally $(document).ready(...) जैसा ही है लेकिन लिखने में छोटा, और यह वही form है जो आप real-world jQuery code और tutorials में सबसे ज़्यादा देखेंगे।

उदाहरण: Modern Shorthand

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="msg">Original</p>
    <script>
      $(function() {
      $("#msg").text("Shorthand ready works too");
      });
    </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. body मौजूद होने से पहले head में $("#msg").text(...) चलाना, इसलिए selector किसी से भी match नहीं करता और jQuery बिना किसी error के चुपचाप fail हो जाता है।
  2. ready callback के बाहर किसी ऐसे element के लिए click handler bind करना जो अभी DOM में नहीं है, इसलिए handler कभी attach नहीं होता।
  3. जब script पहले से body के आख़िर में हो तब भी $(document).ready जोड़ना, या closing }); भूल जाना, जो एक syntax error छोड़ देता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.