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

Attributes पढ़ना और Modify करना

एक HTML attribute पढ़ने के लिए attr() इस्तेमाल करें।
Syntax
javascript
$(selector).attr("name");
$(selector).attr("name", "value");
$(selector).removeAttr("name");

Attributes पढ़ना

attr() method href, src, या title जैसे किसी HTML attribute की current value सीधे markup से पढ़ता है। यह हमेशा HTML में set की गई original value, या attr() से आपने आख़िरी बार explicitly assign की गई value return करता है -- कोई भी state changes नहीं जिन्हें browser internally track करता है।

उदाहरण: Reading Attributes

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <a id="link" href="https://example.com">Link</a>
    <script>
      console.log($("#link").attr("href"));
    </script>
  </body>
</html>

Attributes Set करना

किसी attribute को बदलने के लिए attr() को एक name और एक value pass करें, जैसे किसी image का src या किसी link का href update करना। jQuery markup को तुरंत update करता है, और किसी jQuery collection पर इसे call करना एक ही call में हर matched element को update करता है।

उदाहरण: Setting Attributes

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <img id="pic" src="old.jpg">
    <script>
      $("#pic").attr("src", "new.jpg");
    </script>
  </body>
</html>

कई Attributes

आप एक ही call में कई attributes set करने के लिए attr() को name/value pairs का एक plain object pass कर सकते हैं। यह तब काम का है जब आपको किसी element को configure करना हो -- जैसे किसी img का src, alt, और title -- तीन अलग calls chain करने के बजाय एक साथ।

उदाहरण: Multiple Attributes

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <img id="pic">
    <script>
      $("#pic").attr({src: "photo.jpg", alt: "A photo", title: "My Photo"});
    </script>
  </body>
</html>

Attributes हटाना

removeAttr() किसी attribute को इसे एक खाली string set करने के बजाय element से पूरी तरह हटा देता है। disabled या required जैसी attribute हटाना असल में element की behavior बदल देता है, सिर्फ उसका markup नहीं।

उदाहरण: Removing Attributes

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="field" disabled>
    <script>
      $("#field").removeAttr("disabled");
    </script>
  </body>
</html>

Attribute बनाम Property

href, src, और title जैसे HTML attributes के लिए attr() इस्तेमाल करें -- markup में सीधे लिखी गई values। live DOM state reflect करने वाली properties के लिए, जैसे कोई checkbox इस समय checked है या नहीं, इसके बजाय prop() इस्तेमाल करें, क्योंकि user interaction के बाद ये दोनों अलग हो सकते हैं।

उदाहरण: Attribute vs Property

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="check" type="checkbox" checked>
    <script>
      console.log("attr:", $("#check").attr("checked"), "prop:", $("#check").prop("checked"));
    </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. किसी checkbox की current state पढ़ने के लिए .attr("checked") इस्तेमाल करना, जो original HTML attribute देता है, यह नहीं कि user ने अभी क्या ticked किया है।
  2. .attr("src", "a.jpg", "alt", "x") से कई attributes set करने की कोशिश करना, जबकि कई attributes के लिए .attr({src: "a.jpg", alt: "x"}) जैसा एक object चाहिए।
  3. .attr("href") पढ़ना और पूरे resolved URL की उम्मीद करना, जबकि यह exactly वही return करता है जो markup में लिखा है जैसे page.html।

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.