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:

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:1 Changed 6 years ago by plz821

I need the result is 'Y', how can I get it?

the js example: http://jsfiddle.net/plz821/hT5y6/

Last edited 6 years ago by plz821 (previous) (diff)

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.