1

Need to snag page title and other meta from jQuery's $.get results. Read somewhere on here (can't seem to find the question though - sorry!) that it only returns elements within body. That said, if I log the results to the console I see a massive array of data that includes the title and meta tags.

So the question is simple- how can I navigate the array to pull out the title?

Here's an example. The below example returns content as an HTML object, but title returns undefined. Makes sense if only the body is returned. That said, logging data prints the entire pages HTML from top to bottom. Logging thiis prints the page as an array, where I can see an entry for title, whose innerHTML has the page title. So - how should title's value be assigned?

Thanks for your help!

JavaScript

$.get(target, function(data){

    var thiis   = $(data),
        content = thiis.find('#pageMain'),
        title   = thiis.find('title');

    console.log(data, thiis);
});

2 Answers 2

4

jQuery does not parse the doctype, html, head, and body elements. It sets your whole page that you retireved with the GET request as the innerHTML of a div. You only get back a collection of the resulting element so the title element and id of pageMin are not able to be found with find(). You would need to use filter().

$.get(target, function(data){
    var nodes   = $(data),
        content = nodes.filter('#pageMain'),
        title   = nodes.filter('title');
    console.log(content,title);
});

In order to use find() you would need to set the innerHTML of a div with the data and query off that parent node.

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

1 Comment

Got it- any reason this is more efficient than @scrappedcola's answer below?
1

Wrap the returned data in a div first then make your selection.

$.get(target, function(data){
    var $wrap = $("<div></div>").append(data);
    var title = $wrap.find("title");
});

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.