Tin Nguyen
Tin Nguyen

Reputation: 99

jQuery in a HTML formatted string

I have some AJAX call like this:

$.ajax({
    type: 'GET',
    url: <some url>,
    dataType: 'html',
    success: function(html){
        // do some jQuery on the response html
        // like this: $('#someDIV').text();
    }
});

The response html likes this:

<html>
<head>
    <!-- header things -->
</head>
    <div id="someDIV">
        content
    <div>
</html>

Can I do the query on that response html without append the code to current page?

Upvotes: 3

Views: 1271

Answers (3)

Chetan
Chetan

Reputation: 6901

var content = $(dynamichtml);

Later, you can query it.

var item = content.select({jqueryCriteria});

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

Simply wrap by jQuery and do the rest.

var $html = $(html);

Upvotes: 3

Deep
Deep

Reputation: 9794

Query the response string to find the div element inside it like below

var response = '<html> <head></head> <div id="someDIV"> content <div> </html>'
console.log($(response).find("div"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions