Bug Tracker

Opened 5 years ago

Closed 5 years ago

#15214 closed bug (notabug)

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"));

Change History (2)

comment:1 Changed 5 years ago by dmethvin

Owner: set to jlevene
Status: newpending

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

comment:2 Changed 5 years ago by Rick Waldron

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"};

http://i.gyazo.com/f04f08cc098e2623f863eecf06e18319.png

Last edited 5 years ago by Rick Waldron (previous) (diff)
Note: See TracTickets for help on using tickets.