0

In jQuery documentation (jQuery.data()) examples are regarding "data" assigned to elements that are already appended to document, like this

<script>var div = $("div")[0];
    jQuery.data(div, "test", { first: 16, last: "pizza!" });
    $("span:first").text(jQuery.data(div, "test").first);
    $("span:last").text(jQuery.data(div, "test").last);
</script>

I've tried to assign data in the same way, but to a yet NOT appended object, like this

<script>var div = $("<div></div>")[0];
    jQuery.data(div, "test", { first: 16, last: "pizza!" });
    $("span:first").text(jQuery.data(div, "test").first);
    $("span:last").text(jQuery.data(div, "test").last);
</script>

It does not work. Should it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michael
  • 4,786
  • 11
  • 45
  • 68

1 Answers1

1

Remove the [0], works in latest jquery Release

<script>var div = $("<div></div>");
    jQuery.data(div, "test", { first: 16, last: "pizza!" });
    $("span:first").text(jQuery.data(div, "test").first);
    $("span:last").text(jQuery.data(div, "test").last);
</script>

Proof: http://jsfiddle.net/Lsa7D/1/

worenga
  • 5,776
  • 2
  • 28
  • 50
  • Thank you for the advice, but I have just updated the code, and it is not working for me.. Please take a look at http://jsfiddle.net/strategycon/HT5tf/1/ – Michael Apr 23 '12 at 00:30
  • you're working with local variables which contents are gone when the event is executed again: see http://jsfiddle.net/wEVyU/1/ – worenga Apr 23 '12 at 00:33