Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .data() with fragment

In this jsFiddle: https://jsfiddle.net/cu99jyc2/

Note that after setting .data() on the fragment, it is able to read it back. Also, note that the jQuery version is 1.9.0

Now, if you change the jQuery version to 1.9.1 it fails: https://jsfiddle.net/cu99jyc2/1/

Why is this? Was it actually a bug in old jQuery that it ever worked? If so, what bug exactly? What kinds of nodes are you allowed/not allowed to set .data() on?

like image 697
Dan Avatar asked Dec 23 '15 20:12

Dan


1 Answers

The change that cause the difference of behavior is in acceptData method. If you look at jQuery code, you'll see this comment:

// Do not set data on non-element because it will not be cleared (#8335).

See ticket here: https://bugs.jquery.com/ticket/8335

So they changed the way of validation, so that it accepts only elements, and fragments are not elements. See here if you change this line from 1.9.1, in acceptData method, which validates if setting data should be allowed:

if ( elem.nodeType && elem.nodeType !== 1 && elem.nodeType !== 9 ) {
            return false;
        }

to:

if ( elem.nodeType && elem.nodeType !== 1 && elem.nodeType !== 9 && elem.nodeType !== 11) {
            return false;
        }

You'll access the data.

See: https://jsfiddle.net/gjom6trg/1/

like image 54
Julien Grégoire Avatar answered Oct 14 '22 21:10

Julien Grégoire