user5033350
user5033350

Reputation:

How to work with specific elements of JSON

I have a JSON responsee:

{  
   "errorMessage":0,
   "comments":[  
      {  
         "commentId":37,
         "name":"name",
         "comment":"zxcasdqwerty",
         "timestamp":1460027788,
         "typeOfUser":1,
         "appropriate":0,
         "inappropriate":0,
         "allreadySetApproval":0
      },
      {  
         "commentId":36,
         "name":"name",
         "comment":"123",
         "timestamp":1460027777,        
         "typeOfUser":1,
         "appropriate":0,
         "inappropriate":0,
         "allreadySetApproval":0
      }
   ]
}

So I have upvote/downvote on comments look alike system. This is how it looks temporary: https://i.sstatic.net/rdBSx.jpg.

So what I'm actually trying to achieve is whenever I click on the green or red circle I want to access commentId of comments and use/send it to server. Something like this:

var data = {commentId: idOfComment, typeOfUser: typeOfUser, approvalType: 1 };
                    dataservice.setApprovalOnEventComments(document.cookie.substring(12), data, function () {

                    });

I did this: var idOfComment = res.comments[i].commentId;but that only gives me first commentId from the comments because I have a for loop that gives me all the comments.

for (var i = 0; i < res.comments.length; i++) {
                var text = res.comments[i].comment;
}

More of the code : http://pastebin.com/0XEQywcY

Upvotes: 0

Views: 77

Answers (1)

Michael Oakley
Michael Oakley

Reputation: 383

I would pass the comment ID to the onclick function.

likeComment = function ( commentid ) {

    var data = {commentId: commentid, typeOfUser: typeOfUser, approvalType: 1 };
    dataservice.setApprovalOnEventComments(document.cookie.substring(12), data, function () {

    });
};

And add it to your html.

'<a href="#"><img onclick="likeComment('+ idOfComment  +')"  title="like" class="img-responsive img-circle" src="../../images/my_icon_test_green.png"/></a>' +

Upvotes: 1

Related Questions