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.

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

You're presuming he has the ability to alter what the external script is returning.
@Blazemonger how about now?
You're still altering data without knowing he is able to do so.
@Blazemonger if you mean the id="myID" part then i will change my answer
@Blazemonger is it ok now?
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.