0

I made an ajax call to a page and I receive some HTML code:

$.ajax({
    url: 'test.php',
    data: "id=1",
    cache: false,
    success: function(myHtml){
       //here I have myHtml 
    }
});

the returned html by test.php is myHtml and looks like:

<div id="firstDiv">
some text 123
</div>
<div id="firstDiv">
some text 456
</div>

How I get the content of firstDiv in jquery success ?

Thank you.

4 Answers 4

1

You can use the jQuery constructor to build a jQuery object based on that code.

var results = $(myHtml);

In this case, you will have several elements in the selection, so you'll need to filter them, perhaps with eq in this case:

var firstResult = results.eq(0);

Note that there is no telling what jQuery will do with multiple instances of the same id in an HTML string.

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

Comments

0
$(myHTML).filter("div:first").text()

Comments

0

Try the following:

$("#firstDiv").get().innerHTML

Comments

0
$(myHtml).find("#firstDiv").html()

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.