I have a question about .data(). In a stackoverflow answer, jquery saves data in cache object where cache index is suffixed with a hash of the dom element. For example, consider
I'm not sure you have understood what you have done with the JQUERY selectors.
In your example HTML you nested 2 divs and then placed a single paragraph into the lowest level div. Then in your JQUERY you asked it to look for 2 divs and a paragraph. That's why you got the "correct" result. This actually has nothing to do with .data()
as such, but the fact that there wasn't anything else for JQUERY to find, and therefore the result was always going to be "correct".
However, I'm not certain you would get the "correct" result if you had a larger context with many nested divs that had no id values.
I would say that it is good practice for you to assign an id value for all your In that way you can know what the outcome of your code will be as you create it..data()
.