Bug Tracker

Opened 6 years ago

Closed 6 years ago

#14346 closed bug (notabug)

attr(),prop(): failed to get custom property value

Reported by: plz821@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:


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'


Change History (4)

comment:1 Changed 6 years ago by plz821

Version 0, edited 6 years ago by plz821 (next)

comment:2 Changed 6 years ago by dido

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 6 years ago by dido

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 6 years ago by gibson042

Resolution: notabug
Status: newclosed

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

Note: See TracTickets for help on using tickets.