Opened 8 years ago
Closed 8 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 8 years ago by
Owner: | set to jlevene |
---|---|
Status: | new → pending |
comment:2 Changed 8 years ago by
Resolution: | → notabug |
---|---|
Status: | pending → closed |
$(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"};
Note: See
TracTickets for help on using
tickets.
I'm not sure I'm understanding the problem here. Can you provide a jsfiddle.net example with expected/actual results?