1

I create a new 'li' in the HTML document and after completing the AJAX request (after sending the form) I wish to add the following data to li like this:

<li class="issue" data-winbook-issueId="3">My Issue here</li>

I have the reference of the 'li' under question and I try to do the following:

liReference.data("winbook-issueId", returnedJSONObject.issueId);

If I query this the same in the next line like this:

var id = liReference.data("winbook-issueId");

I get the value of id in alert or in the Debugger. But if I try and access this data in another function like deleteIssue() then id seems to return undefined!!

When the li is initially created there is no data attribute. I'm trying to add a new one in the created node (since the 'node' is created using a micro template engine).

Any ideas what I may be missing? I've attached the code below (the above is the conceptual explanation of the code below). I'm trying to access the value "data-winbook-issueId" and "data-winbook-issueStatus"

function postDataToWall(contentType, dataAreaToReplaceWithContent)
{
    var content = dataAreaToReplaceWithContent.children('.dataForm').val();
    var postDetailsContainer = dataAreaToReplaceWithContent.parent();
    var wcid = postDetailsContainer.parents('li.listOfIssues').data('winbook-wcid');

    var postData ="Issue="+content+"&wcid="+wcid; 

    $.ajax({

    type:"POST",
    url:"/Issue",
    data:postData,
    success:function(result, status){

    switch(contentType.toLowerCase())
    {
            case "Issue".toLowerCase():
        dataAreaToReplaceWithContent.remove();
    postDetailsContainer.append('<ul class="postDetails">'+
                                                   '<li class="issueid">Issue '+result.issueid+':<li>'+
                                                '<li class="postData">'+content+'</li>'+
                                                '<li><ul class="actionsNavBar">'+
                                                '<li><a class="actionNavBarLink" data-winbook-action="CloseIssue">Close Issue</a><span class="dotSeparator">.</span></li>'+
                                                '<li><a class="actionNavBarLink" data-winbook-action="Comment">Comment</a><span class="dotSeparator">.</span></li>'+
                                                '<li><a class="actionNavBarLink" data-winbook-action="Option">Suggest Option/Alternative(s)</a></li>'+
                                                '</ul></li></ul>');

                    var postNode = postDetailsContainer.parents('li.post');                     
                    postDetailsContainer.parents('.dataForm').toggleClass('dataForm');
                    postNode.children('.hoverMenu').toggle(); //enable the hover menu for delete/edit
                    postNode.data("winbook-issueStatus","open");
                    postNode.data("winbook-issueId",result.issueid);

                    $.data(postNode,"winbook-issueStatus","open");

                    someData = postNode.data("winbook-issueId");
                    someData2 = postNode.data("winbook-issueStatus");                       
                    break;
                    }
                },//end success function

                error: function(xhr, status){
                    alert("Status: "+xhr.status+" = "+xhr.statusText+": "+xhr.responseText);
                }   
            });//end ajax

EDIT: The function where the data is used:

<li class="post issue" data-winbook-issueStatus="open" data-winbook-issueId="44">

                            <a class="delete" data-winbook-delete="issue">

                            <img class="hoverButton deleteButton" src="http://localhost:8080/Winbook/images/deleteredicon.png"/>

                            </a>

                            <a class="edit" data-winbook-edit="issue">

                            <img class="hoverButton editButton" src="http://localhost:8080/Winbook/images/editpencil.png"/>

                            </a>

                            <div class="postContainer">...</li>

and it's called in the live('click') handler for a.delete:

$('a.delete').live('click', function() {

var issueIdToDelete = $(this).parent().data("winbook-issueId");

//send ajax request for deletion...but issueIdToDelete is undefined!!

}

NOTE: The problem is ONLY for the node that I want to delete immediately after creation. Nodes created at the server and sent work fine...The above EDIT snippet is copied from the source file that "shows" the data-* attributes but are not visible in the DOM tree as mentioned in the comments below (and rightly pointed out so by @Pointy)

PhD
  • 11,202
  • 14
  • 64
  • 112
  • Where exactly in that code do things not work? Are you 100% sure that "postNode" is a non-empty jQuery object? I'd make sure to check that "postNode.length" is non-zero. – Pointy Jun 19 '11 at 12:55
  • @Pointy:It doesn't work when I query it in another function. I'm sure postNode is not zero since somedata and somedata2 actually have values in them and the node is infact rendered after this!!! I'll double check though...doing that right now :) – PhD Jun 19 '11 at 16:10
  • Well, without seeing this other function, it's going to be pretty hard to see what the problem is. I assure you that the ".data()" mechanism does work. – Pointy Jun 19 '11 at 16:11
  • Nope. length = 1 .... sigh.... – PhD Jun 19 '11 at 16:45
  • Okay, so this is weird. When I view the page source the data-winbook-* attributes are there!! But when I'm seeing the DOM view in Chrome it doesn't seem to show up!! – PhD Jun 19 '11 at 16:54
  • Setting values via ".data()" does **not** create "data-" attributes on elements. – Pointy Jun 19 '11 at 16:58
  • @Pointy: have a look at the edit...I've added the code showing where it is being called and how am I doing it. But it's still undefined!!! – PhD Jun 19 '11 at 17:00
  • Hmm ... where exactly does the dynamic `
  • ` element get the "post" class? I see "postData" in the first code sample, but not "post". What if the "click" function were changed so that it looks for the `
  • ` with `$(this).closest('li.post')` - then you can check to make sure that *that* length is 1 ...
  • – Pointy Jun 19 '11 at 17:31
  • It is generated by default with that class i.e., "post" since it's a part of the template. What you see in the first sample is a nested child of the the `
  • ...
  • ` I am trying to get the outer li (i.e., the post's id) for deletion – PhD Jun 19 '11 at 17:35
  • Okay on further inspection: I checked the server logs and it seems that the DELETE request is indeed being received. But the UI seems to be failing. On Ajax success the following code is called: `function deleteIssue(result, status, issueIdToDelete) { $('[data-winbook-issueId='+issueIdToDelete+']').fadeOut("slow", function() { $(this).remove(); }); }` It seems that **this** is the failing point leading us to believe that deletion is indeed not happening! Can the data in the cache be accessed like that?? (since the data is not created but is a part of the cache at that point) – PhD Jun 19 '11 at 17:39
  • What template? I'm looking at the code that adds the `
      ` block in your first code sample. Where is class "post" in that code?
    – Pointy Jun 19 '11 at 17:42
  • @Pointy: The actual `
  • ...
  • ` is generated with an internal javascript 'micro template' It helps me use the same code for different data types that I'm working with. It'll just make the post too huge if I were to post that template too! The latter code is actually a snippet of that template...just the data is populated/modified by the first code i.e., `postDataToWall(...)` – PhD Jun 19 '11 at 17:52