0

For some reason, the jquery .data() method is only working when the data is set using jquery. Attempting to retrieve data attributes set in the html results in a return value of null or undefined. Here is my javascript and html.

var ico = "<span class='deleteTag'></span>";
var x = "<span class='usertag' data-id='" + ui.item.userid + "'>" + ui.item.label+ " " + ico + "</span>";
$("#" + id + " .userEditor").prepend(x);

$("#" + id + " .userEditor .deleteTag").bind("click",function(){
    alert($(this).parent().attr("data-id"))
    alert($(this).parent().data("id"));
    alert($("#testdivv").data("hola"));
    $(this).parent().data("yolo","swag");
    alert($(this).parent().data("yolo"));                                     
    // $(this).parent().remove();
});
<form method="post">
    <div id="#testdivv" data-hola="hello"></div>
    <div id="user1"><div class="userEditor" style="border:1px solid gray;">
        <input style="border:0px;width:600px;" type="text" onKeyUp="if(event.keyCode != 13 && event.keyCode != 40)userSuggest('user1');"/>
    </div>
    <div class="userSuggestions"></div>
    <input name="IDList"  class="hiddenPut" type="hidden" /></div>
</form>
Casey Falk
  • 2,617
  • 1
  • 18
  • 29
jwesly
  • 19
  • 4

1 Answers1

0

Here's a working JSFiddle.

It works for me in Chrome.

    $(document).ready(function(){

    var id = "editor1";
    var ui = { 
                item: { userid : "user1", label: "some label" }
             };

    var ico = "<a href='#' class='deleteTag'>ClickThisDeleteTag</a>";
    var x = "<span class='usertag' data-id='" + ui.item.userid + "'>" + ui.item.label+ " " + ico + "</span>";

    $("#" + id + " .userEditor").prepend(x);

    $("#" + id + " .userEditor .deleteTag").bind("click",function(){
        $("#answer").html($(this).parent().data("id"));
    });

});
TchiYuan
  • 4,258
  • 5
  • 28
  • 35