panthro
panthro

Reputation: 24061

Turning returned data into a jquery object

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.

Upvotes: 2

Views: 71

Answers (3)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

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.

Upvotes: 4

ramverma
ramverma

Reputation: 1

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

Upvotes: -1

user2560539
user2560539

Reputation:

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.
});

Upvotes: 0

Related Questions