alex
alex

Reputation: 1900

Parsing AJAX response in callback

I need to access a piece of data from some HTML response in an AJAX callback.

In my Coffeescript, I want to access the completion-id data attribute. When I do the following:

  $('.mark-completed')
    .bind "ajax:success", (event, data) ->
        console.log data
        console.log $(data)

I get this in the console for data:

<li class="learning_item_completion" data-completion-id="162" data-user-id="2" id="learning_item_completion_162">
  <div class="item-icon">
    <img alt="Thumb_alex_headshot_green_200x200" class="circular" height="60" src="/uploads/user/picture/2/thumb_alex_headshot_green_200x200.png" width="60" />
  </div>
  <div class="item">

And this for $(data):

[li#learning_item_completion_162.learning_item_completion, jquery: "1.9.1", constructor: function, init: function, selector: "", size: function…]
  0: li#learning_item_completion_162.learning_item_completion
  accessKey: ""
  attributes: NamedNodeMap
  0: class
  1: data-completion-id
    ...
    nodeValue: "162"
    value: "162"
    ...

What's the right way to access completion-id?

Upvotes: 0

Views: 112

Answers (2)

rphonika
rphonika

Reputation: 1121

Simply add this :

$('.mark-completed')
.bind "ajax:success", (event, data) ->
    cid = $(data).find('.learning_item_completion').data('completion-id')
    console.log cid

Upvotes: 1

user2368299
user2368299

Reputation: 369

$.ajax({
  url: "test.html"
}).done(function( html ) {
  $("#results").append(html);
  console.log(html);
//etc.
});

Upvotes: 0

Related Questions