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

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

checked या disabled जैसी DOM properties पढ़ने के लिए prop() इस्तेमाल करें।
Syntax
javascript
$(selector).prop("name");
$(selector).prop("name", value);

Properties पढ़ना

prop() method live DOM property value पढ़ती है, जैसे checked या disabled, जो HTML में originally लिखी गई attribute से अलग हो सकती है। उदाहरण के लिए, किसी checkbox का checked attribute load होने के बाद कभी नहीं बदलता, लेकिन user के click करने पर उसकी checked property update होती है।

उदाहरण: Reading Properties

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($("#check").prop("checked"));
    </script>
  </body>
</html>

Properties Set करना

checked, disabled, या selected जैसी boolean DOM properties बदलने के लिए prop() इस्तेमाल करें। सीधे true या false pass करना element की असली behavior toggle करता है, सिर्फ उसका बाहरी markup नहीं।

उदाहरण: Setting Properties

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">
    <script>
      $("#check").prop("checked", true);
    </script>
  </body>
</html>

Properties Toggle करना

आप किसी property की current value पढ़ सकते हैं और फिर prop() से इसे opposite set कर सकते हैं, यह 'select all' checkboxes जैसे toggle buttons के लिए एक common pattern है। लिखने से पहले पढ़ना आपके code को उस state पर react करने देता है जिसमें user ने element को छोड़ा था।

उदाहरण: Toggle Properties

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">
    <button id="btn">Toggle</button>
    <script>
      $("#btn").on("click", function() {
      $("#check").prop("checked", !$("#check").prop("checked"));
      });
    </script>
  </body>
</html>

Selected Option

prop() form option elements की selected state पढ़ सकता है, आपको यह check करने देते हुए कि किसी select में इस समय कौन-सा option चुना गया है। यह value attribute पढ़ने की कोशिश करने से ज़्यादा reliable है, क्योंकि selection markup को छुए बिना बदल सकता है।

उदाहरण: Selected Option

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <select id="color"><option>Red</option><option selected>Blue</option></select>
    <script>
      console.log($("#color option:selected").text());
    </script>
  </body>
</html>

Property और Attribute

live form state -- checked, disabled, selected -- के लिए, prop() आमतौर पर सही choice है, क्योंकि यह reflect करता है कि browser में अभी असल में क्या हो रहा है। सिर्फ तभी attr() की तरफ़ जाएँ जब आपको specifically original HTML-authored value चाहिए।

उदाहरण: Property and Attribute

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("Live state:", $("#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. user के click करने के बाद किसी checkbox पर .attr("checked", true) इस्तेमाल करना, जो .prop("checked", true) की तरह live state track नहीं करता।
  2. .prop("checked") को string "checked" से compare करना, जबकि यह boolean true या false return करता है।
  3. data-id जैसे custom attributes के लिए .prop() इस्तेमाल करना, जबकि .attr() या .data() सही तरीका है।

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.