Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Prop function in jQuery 1.6

DZone's Guide to

Prop function in jQuery 1.6

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

One of the new features included in jQuery 1.6 version is the new prop function. The prop means property which has ability to find any property on the given selector and retrieve it’s value.

Example

<input id="myTxt" type="text" name="myTextBox" value="This is the input text box value" />

We have four properties: id, type, name, value. Now, we can retrieve any of these values on the following way

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var name = $("#myTxt").prop("name");
        alert(name);
    });
</script>

The result will be myTextBox.

It’s interesting to mention that before v1.6, the job for prop() was done by attr() function. As from jQuery 1.6, the prop() will be used to retrieve property values, while attr() for attributes.

The following example gives clear distinction between these two (from jQuery docs website):

<input type="checkbox" checked="checked" />

$(elem).prop("checked")- true (Boolean)

elem.getAttribute("checked")- "checked" (String)

$(elem).attr("checked")(1.6+) - "checked" (String)

$(elem).attr("checked")(pre-1.6) - true (Boolean)

So, from jQuery v1.6, if we use attr() to get the checkbox checked value, it will return us the string “checked” (if it’s checked). On the other hand, if we use prop() it will return us true which is according to the W3C forms specification.

If you work either with ASP.NET MVC or WebForms, this will be very useful for those working heavily with jQuery.

You can read more about prop() on the following link: http://api.jquery.com/prop/

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Hajan Selmani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}