jQuery – DOM Attributes and Properties

AID: 5430
  • Status: Published

1920 points

  • Byivostoykov
  • TypeReview
  • Posted on2011-05-10 at 05:05:06
DOM Attributes and Properties treatment with jQuery 1.6
by Ivo Stoykov

jQuery 1.6 introduces .prop() and .removeProp() methods which allow modifying or removing a DOM property.


.prop()

method get a property value of the first found element and returns it as a string. If required property has not been set or searched element has not been found returned values will be undefined .

Earlier jQuery versions used to get the value of a property or attribute the .attr() method. Version 1.6 differentiate both attributes and properties supplying method for each of them. For instance .attr(“checked”) of an input type=”checkbox” element will return “checked” or empty string (“”) rather than true/false (as is the case is previous versions).

The preferred way to determine if a checkbox is checked (in cross-browser-compatible way) one of the following might be used:

   
  • if ( el.checked )
  • if ( $(el).prop("checked") )
  • if ( $(el).is(":checked") )


.attr(“checked”) will retrieve the attribute, which does not change but stores the default or initial value of the checked property. Here is a short snippet demonstrating this behavior:

<input id="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>

<script>
$("input").change(function() {
  var $input = $(this);
  $("p").html(".attr('checked'): <b>" + $input.attr('checked') + "</b><br>"
              + ".prop('checked'): <b>" + $input.prop('checked') + "</b><br>"
              + ".is(':checked'): <b>" + $input.is(':checked') ) + "</b>";
}).change();
</script>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window


.prop() accept an string input parameter identifying property to get. In this case method returns a string representing property value.

As a second parameter a value (or function returning desired value) to set is applicable. If function is used within it, the keyword this refers to the current element.
$("#submitBtn").prop("disabled", true);
$("input").prop("checked", true);
                                    
1:
2:

Select allOpen in new window


.prop() should be used instead of the .attr() to set disabled and checked values. The .val() method should be used for getting and setting value.


.removeProp()

Properties set by the .prop() method could be removed by .removeProp() method. The method accept two parameters:

propertyName The name of the property to set.
value  A value to set for the property.

Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element.

Instead set these properties to false using .prop().

This is a nice new feature added to jQuery 1.6 which make coding even more intuitive and easy. It also complies with the W3C forms specification where the boolean attribute as checked attribute is, meaning that property is true if the attribute is present at all, nevertheless it might have empty string or no value at all.
Asked On
2011-05-10 at 05:05:06ID5430
Tags

attribute

,

client-side

,

DOM

,

element

,

javascript

,

jquery

,

js

,

property

,

script

Topic

Jquery

Views
839

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Jquery Experts

  1. leakim971

    144,463

    Master

    0 points yesterday

    Profile
    Rank: Genius
  2. Proculopsis

    51,000

    Master

    0 points yesterday

    Profile
    Rank: Sage
  3. mplungjan

    50,388

    Master

    0 points yesterday

    Profile
    Rank: Savant
  4. BuggyCoder

    29,968

    0 points yesterday

    Profile
    Rank: Sage
  5. basicinstinct

    29,004

    0 points yesterday

    Profile
    Rank: Genius
  6. chaituu

    27,436

    0 points yesterday

    Profile
    Rank: Sage
  7. JonNorman

    27,008

    200 points yesterday

    Profile
    Rank: Master
  8. Kravimir

    23,701

    0 points yesterday

    Profile
    Rank: Genius
  9. hielo

    22,150

    0 points yesterday

    Profile
    Rank: Savant
  10. tommyBoy

    16,864

    0 points yesterday

    Profile
    Rank: Genius
  11. sedgwick

    14,400

    0 points yesterday

    Profile
    Rank: Genius
  12. stevepwales

    14,200

    0 points yesterday

    Profile
    Rank: Master
  13. designatedinitializer

    14,000

    0 points yesterday

    Profile
    Rank: Master
  14. LZ1

    13,800

    0 points yesterday

    Profile
    Rank: Genius
  15. kozaiwaniec

    13,300

    1,500 points yesterday

    Profile
    Rank: Guru
  16. COBOLdinosaur

    11,500

    0 points yesterday

    Profile
    Rank: Genius
  17. anuradhay

    11,068

    0 points yesterday

    Profile
    Rank: Guru
  18. StingRaY

    11,000

    0 points yesterday

    Profile
    Rank: Wizard
  19. atique_ansari

    10,902

    0 points yesterday

    Profile
    Rank: Master
  20. mrh14852

    10,900

    0 points yesterday

    Profile
    Rank: Master
  21. alex_code

    10,250

    0 points yesterday

    Profile
    Rank: Guru
  22. gurvinder372

    9,800

    0 points yesterday

    Profile
    Rank: Genius
  23. _agx_

    9,600

    0 points yesterday

    Profile
    Rank: Genius
  24. nap0leon

    9,400

    0 points yesterday

    Profile
    Rank: Sage
  25. elvin66

    8,200

    1,800 points yesterday

    Profile
    Rank: Sage

Hall Of Fame