Question
jQuery .prop() vs .attr(): Understanding Properties and Attributes
Question
In jQuery 1.6, the new .prop() method was introduced. I want to understand when I should use .prop() instead of .attr().
For example:
$(selector).click(function () {
// Native DOM API
this.getAttribute('style');
// jQuery alternatives
$(this).prop('style');
$(this).attr('style');
});
In this case, do .prop('style') and .attr('style') return the same thing?
Also, if .prop() is now the correct method in some situations, will older .attr() code break after upgrading to jQuery 1.6?
Here is a fuller example:
var selector = '#id';
$(selector).click(function () {
var getAtt = this.getAttribute('style');
var thisProp = $(this).prop('style');
var thisAttr = $(this).attr('style');
console.log(getAtt, thisProp, thisAttr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id="id" style="color: red; background: orange;">test</div>
When this runs, getAttribute('style') returns a string, .attr('style') returns a string, but .prop('style') returns a CSSStyleDeclaration object. Why does that happen, and how should this affect the way I write jQuery code going forward?
Short Answer
By the end of this page, you will understand the difference between HTML attributes and DOM properties in jQuery, why .attr() and .prop() can return different values, and when each method should be used. You will also see why style, checked, selected, and value often behave differently and how to avoid bugs when upgrading or maintaining older jQuery code.
Concept
Attributes and properties are related, but they are not the same thing.
- Attributes are the values written in the HTML markup.
- Properties are the live values on the DOM element object in JavaScript.
For example, in this HTML:
<input type="checkbox" checked>
- The
checkedattribute says the checkbox starts checked. - The
checkedproperty tells you whether the checkbox is currently checked right now.
That difference matters because the browser turns HTML into DOM objects. Once the page is loaded, JavaScript works mostly with properties, not raw attribute text.
In jQuery:
.attr()is for reading or writing HTML attributes..prop()is for reading or writing DOM properties.
This is why your style example behaves differently:
this.getAttribute('style'); // string from HTML attribute
$(this).attr();
$().();
Mental Model
Think of an HTML element like a house listing and the actual house.
- The attribute is the information written in the listing: "This house has blue walls."
- The property is the real current state of the house when you visit it: maybe the walls are now green.
For style:
.attr('style')is like reading the original text written on the listing..prop('style')is like walking into the house and inspecting the real paint controls panel.
Another example with a checkbox:
.attr('checked')asks, "Did the HTML say this started checked?".prop('checked')asks, "Is it checked right now?"
That is the key idea: attributes describe markup, properties describe live state.
Syntax and Examples
Core syntax
$(element).attr(name)
$(element).attr(name, value)
$(element).prop(name)
$(element).prop(name, value)
Example 1: style
<div id="box" style="color: red; background: orange;">Hello</div>
var el = $('#box');
console.log(el.attr('style'));
// "color: red; background: orange;"
console.log(el.prop('style'));
// CSSStyleDeclaration object
Explanation
.attr('style')returns the raw value of thestyleattribute as text..prop('style')returns the DOM object.
Step by Step Execution
Consider this example:
<div id="id" style="color: red; background: orange;">test</div>
$('#id').click(function () {
var getAtt = this.getAttribute('style');
var thisProp = $(this).prop('style');
var thisAttr = $(this).attr('style');
console.log(getAtt);
console.log(thisProp);
console.log(thisAttr);
});
What happens step by step
1. The browser parses the HTML
It sees:
style="color: red; background: orange;"
That text is stored as an HTML attribute.
2. The browser creates a DOM element object
Real World Use Cases
When .attr() is useful
Reading markup configuration
<a id="docs" href="/guide">Guide</a>
console.log($('#docs').attr('href'));
Useful when you need values defined in HTML.
Working with custom attributes
<div id="card" data-role="admin"></div>
console.log($('#card').attr('data-role'));
When .prop() is useful
Reading form control state
Real Codebase Usage
In real projects, developers usually follow a few practical rules.
1. Use .prop() for boolean UI state
Common examples:
checkedselecteddisabled
if ($('#rememberMe').prop('checked')) {
savePreference();
}
This avoids bugs where code reads the original HTML instead of the current state.
2. Use .attr() for static element metadata
Examples:
idhrefsrcalttitledata-*when not using.data()
imageUrl = $().();
Common Mistakes
Mistake 1: Using .attr() for checkbox state
Broken idea:
if ($('#newsletter').attr('checked')) {
console.log('Checked');
}
Why it is a problem:
- It may reflect the original HTML attribute, not the current user-selected state.
Better:
if ($('#newsletter').prop('checked')) {
console.log('Checked');
}
Mistake 2: Expecting .prop('style') to return a string
Broken expectation:
var styleText = $('#box').prop('style');
console.log(styleText.toUpperCase());
Why it fails:
.prop('style')returns a object, not a string.
Comparisons
.attr() vs .prop()
| Feature | .attr() | .prop() |
|---|---|---|
| Reads HTML attribute | Yes | No |
| Reads DOM property | No | Yes |
| Best for original markup values | Yes | No |
| Best for live UI state | No | Yes |
| Returns string-like attribute value | Usually | Not necessarily |
| Can return objects | Rarely | Yes |
Common examples
Cheat Sheet
Quick rules
- Use
.attr()for HTML attributes. - Use
.prop()for DOM properties. - Use
.val()for form values. - Use
.css()for CSS values.
Good defaults
$(el).attr('href')
$(el).attr('data-id')
$(el).prop('checked')
$(el).prop('disabled')
$(el).prop('selected')
$(el).val()
$(el).css('color')
style specifically
$(el).attr('style') // "color: red; background: orange;"
$(el).prop('style') // CSSStyleDeclaration object
Boolean properties
Prefer .prop() for:
FAQ
What is the difference between jQuery .attr() and .prop()?
.attr() works with HTML attributes from markup. .prop() works with live DOM properties on the element object.
Should I use .prop('checked') or .attr('checked')?
Use .prop('checked') for the current checkbox state.
Why does .prop('style') return an object instead of a string?
Because the DOM style property is a CSSStyleDeclaration object, not raw attribute text.
Is .attr('style') the same as getAttribute('style')?
For this use case, yes. Both read the style attribute as text.
Will old .attr() code break in jQuery 1.6?
Not all of it. But code that relied on .attr() for properties like checked, , or should be reviewed.
Mini Project
Description
Build a small settings panel with a checkbox, a text input, and a colored preview box. The project demonstrates when to use .attr(), .prop(), .val(), and .css() in realistic jQuery code.
Goal
Create a page that reads static attributes, reacts to live form state, and updates styles correctly using the right jQuery methods.
Requirements
- Add a checkbox that enables or disables a text input.
- Add a text input whose current value is shown in a preview area.
- Add a preview box with an initial inline style and display both its
styleattribute and a live CSS property. - Use
.prop()for enabled/disabled and checked state. - Use
.val()for the input value and.css()for reading or updating CSS.
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.