razvan
razvan

Reputation: 1

Content of html div using jquery

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.

Upvotes: 0

Views: 84

Answers (4)

Luke
Luke

Reputation: 1233

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

Upvotes: 0

Hasan Fahim
Hasan Fahim

Reputation: 3885

Try the following:

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

Upvotes: 0

James Montagne
James Montagne

Reputation: 78630

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

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237837

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.

Upvotes: 1

Related Questions