Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery .prop() Method

Posted in jQuery Tutorial
Updated on May 09, 2024
By Mari Selvan
👁️ 6 - Views
⏳ 4 mins
💬 0
jQuery .prop() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery is renowned for its simplicity and power in web development, offering developers a plethora of tools to manipulate HTML elements effortlessly. One such tool is the .prop() method, which allows you to get or set properties of HTML elements. Understanding and mastering this method can significantly enhance your ability to create dynamic and interactive web pages.

In this comprehensive guide, we'll explore the usage of the jQuery .prop() method with clear examples to illustrate its versatility.

🧠 Understanding .prop() Method

The .prop() method in jQuery is primarily used to get or set properties of HTML elements. It provides a convenient way to access and modify various attributes and properties of elements, such as checked, disabled, or selected states.

💡 Syntax

The syntax for the .prop() method is straightforward:

syntax.js
Copied
Copy To Clipboard
$(selector).prop(propertyName, value)

📝 Example

  1. Getting and Setting the Checked State of Checkboxes:

    The .prop() method is commonly used to manipulate the checked state of checkboxes. Here's how you can use it to check or uncheck checkboxes:

    index.html
    Copied
    Copy To Clipboard
    <input type="checkbox" id="checkbox1">
    <input type="checkbox" id="checkbox2" checked>
    example.js
    Copied
    Copy To Clipboard
    // Get the checked state of checkbox1
    var isChecked = $("#checkbox1").prop("checked");
    console.log(isChecked); // Output: false
    
    // Set the checked state of checkbox1 to true
    $("#checkbox1").prop("checked", true);
    
    // Set the checked state of checkbox2 to false
    $("#checkbox2").prop("checked", false);
  2. Disabling and Enabling Form Elements:

    You can also use .prop() to disable or enable form elements dynamically:

    index.html
    Copied
    Copy To Clipboard
    <input type="text" id="textInput">
    <button id="disableButton">Disable Text Input</button>
    <button id="enableButton">Enable Text Input</button>
    example.js
    Copied
    Copy To Clipboard
    // Disable the text input
    $("#disableButton").click(function() {
      $("#textInput").prop("disabled", true);
    });
    
    // Enable the text input
    $("#enableButton").click(function() {
      $("#textInput").prop("disabled", false);
    });
  3. Manipulating Other Properties:

    The .prop() method is not limited to just checked and disabled states. You can use it to manipulate various other properties such as selected for options in select elements, readonly for text inputs, and value for input fields.

  4. Understanding the Difference Between .prop() and .attr():

    It's important to note the difference between .prop() and .attr(). While .prop() deals with properties and states of elements, .attr() deals with attributes in the HTML markup. Understanding when to use each method is crucial for efficient DOM manipulation.

🎉 Conclusion

The jQuery .prop() method is a versatile tool for manipulating properties and states of HTML elements dynamically. Whether you need to manage the checked state of checkboxes, disable form elements, or manipulate other properties, .prop() provides a convenient and efficient solution.

By mastering its usage, you can create more interactive and responsive web pages with ease.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy