Opened 9 years ago
Closed 9 years ago
#14346 closed bug (notabug)
attr(),prop(): failed to get custom property value
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.10.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
html: <table><tr id='tr1' open='Y'><td>open</td></tr></table>
use: $('#tr1').attr('open'), result is 'open' $('#tr1').prop('open'), result is 'undefine'
Why?
Change History (4)
comment:2 Changed 9 years ago by
This seems to be working as expected as per the .prop() docs, search for "Attributes vs. Properties".
There's a difference between HTML attributes and DOM properties. This StackOverflow answer (and all the other answers above it) should help clear it up.
comment:3 Changed 9 years ago by
I read your question incorrectly the first time. Sorry about that. My previous comment above still stands though.
Your jsfiddle link won't load for me so I created a new one.
The behavior here of .prop() is expected. On executing .attr('open'), Sizzle.attr gets called twice, returning "Y" the first time and then "open" the next. As for whether this is expected, we'll have to wait for official word on that.
Work around this for now by using HTML5 data-* attributes or use .getAttribute on the DOM node itself if you really need to use custom attributes.
comment:4 Changed 9 years ago by
Resolution: | → notabug |
---|---|
Status: | new → closed |
The behavior is expected because open is an HTML5 boolean attribute: https://github.com/jquery/jquery/blob/1.x-master/src/attributes/attr.js#L145-L171
I need the result is 'Y', how can I get it?
the js example: http://jsfiddle.net/plz821/hT5y6/