← Back to jQuery Course | Chapter 5: DOM Manipulation | Lesson 1 of 9

Text HTML और Values Get और Set करना

किसी element के अंदर का plain text पढ़ने के लिए text() इस्तेमाल करें।
Syntax
javascript
$(selector).text();
$(selector).text("new text");
$(selector).html("<b>new html</b>");
$(selector).val();
$(selector).val("new value");

Text पढ़ना

text() method किसी element के plain-text content को वापस पढ़ता है, अंदर मौजूद किसी भी HTML tags को हटाते हुए -- इसलिए element के अंदर एक <strong> tag text() के लिए invisible है, सिर्फ उसके शब्द return होते हैं।

उदाहरण: Reading Text

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><strong>Bold</strong> text</div>
    <script>
      console.log($("#box").text());
    </script>
  </body>
</html>

Text Set करना

text() को एक string argument के साथ call करना किसी element के पूरे content को उस plain text से replace कर देता है, किसी भी HTML-special characters को automatically escape करते हुए ताकि वे markup की तरह parse होने के बजाय literally display हों।

उदाहरण: Setting Text

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"></div>
    <script>
      $("#box").text("<b>Not bold</b>");
    </script>
  </body>
</html>

HTML पढ़ना

html() किसी element के पूरे inner markup को वापस पढ़ता है, किसी भी nested tags सहित, जो तब चाहिए होता है जब आपको सिर्फ शब्दों के बजाय structured content inspect या copy करना हो।

उदाहरण: Reading HTML

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><strong>Bold</strong> text</div>
    <script>
      console.log($("#box").html());
    </script>
  </body>
</html>

HTML Set करना

html() को एक string argument के साथ call करना किसी element के contents को उस markup से replace कर देता है, इसे असली HTML की तरह parse करते हुए -- text() के उलट, string में कोई भी tags page में live elements बन जाते हैं।

उदाहरण: Setting HTML

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"></div>
    <script>
      $("#box").html("<strong>Now bold</strong>");
    </script>
  </body>
</html>

Values Get और Set करना

val() किसी form control जैसे input, select, या textarea की current value पढ़ता या set करता है, जो text() और html() से अलग है क्योंकि किसी form control की displayed value को regular element content की तरह store नहीं किया जाता।

उदाहरण: Getting and Setting Values

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="name" type="text" value="Alice">
    <script>
      console.log($("#name").val());
      $("#name").val("Bob");
    </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. user input से आने वाले text के साथ .html() इस्तेमाल करना, जो उसमें मौजूद किसी भी tags या scripts को parse होने देता है; .text() उन्हें escape करता है।
  2. यह उम्मीद करना कि किसी <div> पर .text() उसके inner tags रखेगा, जबकि यह सिर्फ text return करता है और <strong> जैसे markup हटा देता है।
  3. किसी input पर .text() इस्तेमाल करना और कुछ न मिलना; form controls अपनी current value .val() में रखते हैं।

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.