2

I have a promise on a get request in jquery, that return data then add it to the page via append:

$.get('/get/something').done(function(data) {
    $('#main').append(data);
})

The data looks something like :

<div class="container">
    ...
    ...
</div>

How can I wrap that returned data/appended element in a jquery selector and then perform operations on it?

I've tried :

$(data).some-jquery-fnc();

But no luck.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
panthro
  • 22,779
  • 66
  • 183
  • 324

3 Answers3

4

You could use the class selector with the :last selector :

$('#main .container:last').some-jquery-fnc();

That will always select the last .container added to the #main by the .append() method :

$('#main').append(data);

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
0

If you append only one element and want to bind your actions with an event this might be of use.

$(document).on("click",".container",function() { // or on mouseover or any other event you want
// rest of your code to be executed for this particular event.
});
-1

You can use: $.parseHTML(data) or $('').html(data).contents(); For detail, please refer: Creating a jQuery object from a big HTML-string

Community
  • 1
  • 1
ramverma
  • 1
  • 1