4

In jQuery, why does this:

$('<div open="whatever">').attr('open')

Always evaluate to 'open' instead of 'whatever'? In contrast, this:

$('<div asdf="whatever">').attr('asdf')

Evaluates to 'whatever' as expected.

Yes, I am aware that open and asdf are not valid HTML attributes; I'm not looking for answers that say something along the lines of "just use data-open", etc... I am looking for an explanation of the above behavior.

Ben Lee
  • 52,489
  • 13
  • 125
  • 145
  • Why are you trying to do this in the first place? It's probably undocumented behavior. – LandonSchropp Mar 01 '12 at 22:45
  • @helixed haven't you ever just been curious? ;-) Ben may have been using the arbitrary attribute (modern browsers will support this) and when his application didn't work, he eventually noticed it was the `open` attribute and wondered "huh? why?" – Greg Pettit Mar 01 '12 at 22:51
  • @helixed, I noticed when working with some html for legacy code base that used the bare `open=` attribute. So the answer is "I wouldn't be trying to do this in the first place, I'd use a data- attribute". – Ben Lee Mar 01 '12 at 22:54
  • @Ben Lee I wasn't trying to criticize you for asking. I was just wondering under what circumstances you would want to know this. – LandonSchropp Mar 02 '12 at 01:13

2 Answers2

5

open is actually a valid attribute for HTML5 that's meant to be a boolean. If that's the case, I suspect that if you have "open" set at all, the browser is evaluating it as true and returning that it is "open".

I'd be more curious to know why it's returning "open" instead of "true". Probably due to incomplete implementation in various browsers. In one way or another they may have set that attribute aside for later.

[addendum: as per comments, this seems to be the way HTML treats booleans]

wal
  • 17,409
  • 8
  • 74
  • 109
Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
  • 2
    Isn't that how HTML treats booleans? `checked="checked"`, etc? – Dagg Nabbit Mar 01 '12 at 22:54
  • Could be! I actually haven't thought about it that way. In which case it's behaving the way it should. – Greg Pettit Mar 01 '12 at 22:55
  • Thanks, I had no idea "open" was a valid attribute! I'm having trouble finding it in the html5 specs though, do you by chance have a link? (If not, that's fine) – Ben Lee Mar 01 '12 at 22:55
  • Well, I'm embarrased to say it because I know the scorn it gets around here, but I stumbled across it here: http://www.w3schools.com/html5/att_details_open.asp -- and if I'm understanding correctly it's only supposed to be valid for a "details" tag. But incomplete or incorrect implementations being what they are... ;-) – Greg Pettit Mar 01 '12 at 22:57
  • Aha: http://www.w3.org/TR/2011/WD-html5-20110525/interactive-elements.html#the-details-element – Ben Lee Mar 01 '12 at 23:00
  • But yeah, just the "details" tag. – Ben Lee Mar 01 '12 at 23:00
0

Did some testing, and for some reason this seems to work:

document.getElementById('myID').getAttribute('open');

While jQuery does not work for neither .prop() or .attr()

Strange indeed?

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388