Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3681314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:40:35+00:00 2026-05-19T03:40:35+00:00

i have an ajax call and i am trying to parse the returned page

  • 0

i have an ajax call and i am trying to parse the returned page for #viewport, thereby removing the header and footer of the returned page. but find() cannot locate the div.

my original function:

        function(event) {
            $.ajax({
                type: this.method,
                url: this.action,
                data: $(this).serialize(),
                datatype: "html",
                success: function(data) {
                    alert(data);
                    var respHTML = $(data).find("#viewport");
                    alert(respHTML.length);
                    $("#contacts_sidebar").html(respHTML);
                }
            });
            return false;
        }

in the alert(data), i definitely see <div id="viewport">, but alert(respHTML.length) displays 0. if i change the selector to “table.someclass”, it would locate it. but selectors such as “head” and “body” returns 0 as well.

i know it’s definitely there b/c i replace the success handler with the following workaround:

        success: function(data) {
            var respHTML;
            var d=$(data);
            for (i=0; i<d.length; i++) {
                if (d[i]["id"] === "viewport") {
                    respHTML = d[i]["innerHTML"];
                    break;
                }
            }
            $("#contacts_sidebar").html(respHTML);
        }

am i missing something? the workaround works, just ugly.

thanks very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:40:35+00:00Added an answer on May 19, 2026 at 3:40 am

    If '#viewport' is at the top level of the response HTML, you’ll need .filter() instead, since .find() only look inside the top level elements.

       success: function(data) {
            alert(data);
            var respHTML = $(data).filter("#viewport");
            alert(respHTML.length);
            $("#contacts_sidebar").html(respHTML);
        }
    

    When passing HTML to jQuery, you can’t rely on finding the <html> <head> <body> tags in a cross browser manner.

    If your response does include an entire HTML document, I would pare it down to only the content you actually want if at all possible.

    Another option may be to do something like this:

    var respHTML = $('<div>' + data + '</div>').find("#viewport");
    

    …but I wouldn’t guarantee the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have been trying to make a ajax call to a JSP page.
I have an ajax call that fires multiple times but is called once. function
I'm trying to refactor this code. I have this jquery ajax call $.ajax({ url:
I ‘m trying to make an Ajax call to an aspx page On the
I'm trying to create simple Ajax call, but after clicking the link I get
I have an ajax call that returns a HTML fragment. I am trying to
I have a subdomain website (mobile) that is trying to call a aspx page
i have an ajax call $.ajax({ url: '<%=Url.Action(SaveDetails,Survey) %>', dataType: 'JSON', cache: false, data:
I have an AJAX call getting info out of the Github API. It is
I have an ajax call. I put the return value (data) in a variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.