Introduction:

In this article we will explain how to use prop() in jQuery or why use prop() in jQuery or how to set get  property value of selected html element using jquery with example.

Description:

The jQuery prop() method is used to get or set value of selected html elements i.e. DOM properties (like tagName, nodeName, defaultChecked) or own custom made properties. This is a very convenient way to set the values of properties, especially the multiple properties.

Syntax:

To get the value of a property:

$(selector).prop(property)  

To set the property and value:

 

$(selector).prop(property,value)  

 

Example of jQuery prop() method 

 

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Jquery Tutorials</title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
</head>
<body>
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $('#Checkbox1').prop("checked", true)
                $('#txtvalue').text($('#Text1').prop("value"));
            });
        });
    </script>
    <input id="Checkbox1" type="checkbox"  /><br />
    <input id="Text1" type="text" value="Aditya" /><br /><br />
    <button>Click here to get and set value</button><br />
    <br />
    <div id="txtvalue"></div>
</body>
</html>

 

OutPut:

 Use jQuery prop()

When you click on button then your checkbox will be checked and alse textbox value show inside “txtvalue” div like shown as below :

 Use jQuery prop()

I hope it will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like