Why does jQuery always return 'open' for $ .attr ('open'), regardless of the actual value of the attribute?

In jQuery, why is this:

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

Always consider 'open' instead of 'whatever' ? On the contrary, it is:

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

Computes to 'whatever' as expected.

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

+4
source share
2 answers

open actually a valid attribute for HTML5 , which should be logical. If so, I suspect that if you have an β€œopen” set at all, the browser evaluates it as true and returns that it is β€œopen”.

I would be curious to know why it returns "open" instead of "true". Probably due to incomplete implementation in different browsers. Somehow, they may set aside this attribute for later use.

[addition: according to the comments, this is similar to how HTML handles booleans]

+5
source

Was there some kind of testing, and for some reason this works:

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

So far jQuery does not work for either .prop () nor for .attr ()

Is it weird?

Fiddle

0
source

Source: https://habr.com/ru/post/1399271/


All Articles