3

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?

2
  • If I undestood correctly your question, you can normally do var newHtml = $(html); and manipulate the variable without appending it to the body. Commented Jan 11, 2017 at 16:49
  • 1
    Please be aware that $(html) will flatten the DOM and remove elements like HEAD and BODY and any SCRIPT tags. Commented Jan 11, 2017 at 16:53

3 Answers 3

3

Simply wrap by jQuery and do the rest.

var $html = $(html);
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0
var content = $(dynamichtml);

Later, you can query it.

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

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.