Skip to main content

Bug Tracker

Side navigation

#15214 closed bug (notabug)

Opened August 18, 2014 02:14PM UTC

Closed August 18, 2014 03:57PM UTC

Bug with .clone() data types

Reported by: jlevene Owned by: jlevene
Priority: high Milestone: None
Component: manipulation Version: 2.1.0
Keywords: Cc:
Blocked by: Blocking:
Description

According to jquery documentation, a data attribute is an object, however .clone() converts these to a "[object]" string.

example:

$(element).data("stuff", {"field1":"1", "field2":"2"});
cloned = $(element).clone();
alert(JSON.stringify(cloned.data("stuff"));

Also creating an element using jquery converts the data to a string as well.

$("<div>", {
"data-stuff" : {"field1":"1", "field2":"2"})
.appendTo(body);
alert(JSON.stringify($("div").data("stuff"));
Attachments (0)
Change History (2)

Changed August 18, 2014 02:22PM UTC by dmethvin comment:1

owner: → jlevene
status: newpending

I'm not sure I'm understanding the problem here. Can you provide a jsfiddle.net example with expected/actual results?

Changed August 18, 2014 03:57PM UTC by rwaldron comment:2

_comment0: \ > {{{ \ > $(element).data("stuff", {"field1":"1", "field2":"2"}); \ > cloned = $(element).clone(); \ > alert(JSON.stringify(cloned.data("stuff")); \ > }}} \ \ There are a few things wrong here: \ \ - Data isn't cloned unless explicitly instructed: http://jsfiddle.net/rwaldron/b0ut0pkp/ \ - clone does not serialize data: https://github.com/jquery/jquery/blob/master/src/manipulation.js#L114-L120 \ - There is a syntax error, but that's irrelevant \ \ \ \ > {{{ \ > $("<div>", { \ > "data-stuff" : {"field1":"1", "field2":"2"}) \ > .appendTo(body); \ > alert(JSON.stringify($("div").data("stuff")); \ > }}} \ \ This isn't a jQuery bug. Attribute properties provided by a context object are subject to the rules of HTML, so the same would happen if you wrote: \ \ var div = document.createElement("div"); \ div.dataset.stuff = {"field1":"1", "field2":"2"}; \ \ \ [[Image(http://i.gyazo.com/f04f08cc098e2623f863eecf06e18319.png)]]1408377482776416
resolution: → notabug
status: pendingclosed
> $(element).data("stuff", {"field1":"1", "field2":"2"});
> cloned = $(element).clone();
> alert(JSON.stringify(cloned.data("stuff"));
> 

There are a few things wrong here:

> $("<div>", {
>  "data-stuff" : {"field1":"1", "field2":"2"})
>  .appendTo(body);
> alert(JSON.stringify($("div").data("stuff"));
> 

This isn't a jQuery bug. Attribute properties provided by a context object are subject to the rules of HTML, so the same would happen if you wrote:

var div = document.createElement("div");
div.dataset.stuff = {"field1":"1", "field2":"2"};

[[Image(http://i.gyazo.com/f04f08cc098e2623f863eecf06e18319.png)]]