Forgive me for not being more specific on this. I have such a strange bug. After the doc loads, I loop some elements that originally have data-itemname=""
, and I set those values using .attr("data-itemname", "someValue")
.
Issue: When I later loop thru those elements, if I do elem.data().itemname
, I get ""
, but if I do elem.attr("data-itemname")
, I get "someValue"
. It's like jQuery's .data()
getter only gets elements that are set initially to contain some value, but if they are originally empty, and later set, then .data()
doesn't get the value later on.
I've been trying to recreate this bug but have not been able to.
Edit
I have recreated the bug! http://jsbin.com/ihuhep/edit#javascript,html,live
Also, snippets from above link...
JS:
var theaters = [
{ name: "theater A", theaterId: 5 },
{ name: "theater B", theaterId: 17 }
];
$("#theaters").html(
$("#theaterTmpl").render(theaters)
);
// DOES NOT WORK - .data("name", "val") does NOT set the val
var theaterA = $("[data-theaterid='5']");
theaterA.find(".someLink").data("tofilllater", "theater5link"); // this does NOT set data-tofilllater
$(".someLink[data-tofilllater='theater5link']").html("changed link text"); // never gets changed
// WORKS - .attr("data-name", "val") DOES set val
var theaterB = $("[data-theaterid='17']");
theaterB.find(".someLink").attr("data-tofilllater", "theater17link"); // this does set data-tofilllater
$(".someLink[data-tofilllater='theater17link']").html("changed link text");
HTML:
<body>
<div id="theaters"></div>
</body>
<script id="theaterTmpl" type="text/x-jquery-tmpl">
<div class="theater" data-theaterid="{{=theaterId}}">
<h2>{{=name}}</h2>
<a href="#" class="someLink" data-tofilllater="">need to change this text</a>
</div>
</script>