6

This is a super simple question that I just can't seem to find a good answer too.

$.get('/myurl.html', function(response){
     console.log(response); //works!
     console.log( $(response).find('#element').text() ); //null :(
}, 'html');

I am just trying to traverse my the html response. So far the only thing I can think of that would works is to regex to inside the body tags, and use that as a string to create my traversable jQuery object. But that just seems stupid. Anyone care to point out the right way to do this?

Maybe its my html?

<html> 
    <head> 
       <title>Center</title> 
    </head> 
    <body> 
        <!-- tons-o-stuff -->
    </body>
</html>

This also works fine but will not suit my needs:

$('#myelem').load('/myurl.html #element');

7
  • 1
    If you're looking for some element in the response html with an id attribute with the value "element", then it looks correct to me. I am not sure why that wouldn't work. Commented Apr 5, 2012 at 16:44
  • 1
    How does the html that you are fetching look like? Commented Apr 5, 2012 at 16:44
  • This should answer your question: stackoverflow.com/questions/9540218/a-javascript-parser-for-dom/… Commented Apr 5, 2012 at 16:46
  • Show us the fetched HTML Commented Apr 5, 2012 at 16:48
  • I think it is failing because it doesn't like the html, head and body tags. Commented Apr 5, 2012 at 16:50

4 Answers 4

7

It fails because it doesn't like <html> and <body>.

Using the method described here: A JavaScript parser for DOM

$.get('/myurl.html', function(response){
     var doc = document.createElement('html');
     doc.innerHTML = response;

     console.log( $("#element", doc).text() );
}, 'html');

I think the above should work.

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

3 Comments

I would need to look at the inner detail of how jquery works to answer that. Basically jquery has a way on how it builds dom objects when when an html string argument is given. This was is simple not compatible with html strings that contain body and html tags, but fortunately it is not the only way and the way above is a different way of doing it.
ok now I have a problem. IE says SCRIPT600: Invalid target element for this operation. on doc.innerHTML
As with everything else, IE needs workarounds: stackoverflow.com/questions/7474710/…
1

When jQuery parses HTML it will normally strip out the html and body tags, so if the element you are searching for is at the top level of your document structure once the html and body tags have been removed then the find function may not be able to locate the element you're searching for.

See this question for further info - Using jQuery to search a string of HTML

Comments

0

Try this:

$("#element", $(response)).text()

This searches for the element ID in the $(response) treating $(response) as a DOM object.

1 Comment

response.responseText is undefined
-1

Maybe I'm misunderstanding something, but

.find('#element')

matches elements with an ID of "element," like

<p id="element">

Since I don't see the "tons of stuff" HTML I don't understand what elements you're trying to find.

1 Comment

This isn't an answer David. It'd be more appropriate to have posted this as a comment.

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.