You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the data() function, then jQuery will get the attribute value if it doesn't exist, and get the data and NOT attribute value if it doesn't exist.
Heres an example:
If you fire $("div").data("foo");, then it will return "house".
That makes you believe that the data("foo") function is linked to the data-foo attribute in the DOM.
So if you set it now like this: $("div").data("foo", "bike");
Now you think that the data-foo attribute value is "bike", haha, thats not the case, its still "house".
So now, to get the attribute data-foo, you need to write $("div").attr("data-foo");
And to get the foo data in the code that i set, then i need to go for $("div").data("foo").
That is EXTREMELY error prone, because there could be name classes between the $("div").data("foo") and the $("div").attr("data-foo");
An example could be if you wanted to check if a key/value pair is undefined in the data(). But it is defined if the DOM object has the attribute and it is undefined if it doesn't have that attribute.
I figured it out when i was coding, and i expected something to be undefined, but it was defined because i had the same data-XXX attribute name as i had in the data("XXX") jQuery function. And then all the bug hell broke lose.
Is this a bug or a stupid intend? I think its very error-prone.
@ Assassinbeast - I think you are the only one that's confused about this... it's been around for years and works as expected, and with good logic behind.
You need to change the way you think when you code, that's all.
seems like you get confused over some basic matters.
Description
When using the data() function, then jQuery will get the attribute value if it doesn't exist, and get the data and NOT attribute value if it doesn't exist.
Heres an example:
If you fire $("div").data("foo");, then it will return "house".
That makes you believe that the data("foo") function is linked to the data-foo attribute in the DOM.
So if you set it now like this: $("div").data("foo", "bike");
Now you think that the data-foo attribute value is "bike", haha, thats not the case, its still "house".
So now, to get the attribute data-foo, you need to write $("div").attr("data-foo");
And to get the foo data in the code that i set, then i need to go for $("div").data("foo").
That is EXTREMELY error prone, because there could be name classes between the$("div").data("foo") and the $ ("div").attr("data-foo");
An example could be if you wanted to check if a key/value pair is undefined in the data(). But it is defined if the DOM object has the attribute and it is undefined if it doesn't have that attribute.
I figured it out when i was coding, and i expected something to be undefined, but it was defined because i had the same data-XXX attribute name as i had in the data("XXX") jQuery function. And then all the bug hell broke lose.
Is this a bug or a stupid intend? I think its very error-prone.
Link to test case
https://jsfiddle.net/ahmdgm1r/
The text was updated successfully, but these errors were encountered: