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

  • Home
  • SEARCH
  • 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 6011909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:19:38+00:00 2026-05-23T02:19:38+00:00

I was reading w3schools and found this example: <body> <p>Hello World!</p> <p>The DOM is

  • 0

I was reading w3schools and found this example:

<body>

<p>Hello World!</p>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>length</b> property.</p>

<script type="text/javascript">
x=document.getElementsByTagName("p");

document.write("------<br />");
for (i=0;i<x.length;i++)
  { 
  document.write(x[i].innerHTML);
  document.write("<br />");
  }
document.write("------");
</script>
</body>

which works just fine.
Then I thought doing the same with jQuery with

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Demo Page</title>

<script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="code.js"></script>
  </head>

and then in the code.js file have

$(document).ready(function()          {

      x=document.getElementsByTagName("p");

        document.write("------<br />");
        for (i=0;i<x.length;i++)
          { 
          document.write(x[i].innerHTML);
          document.write("<br />");
          }
        document.write("------");

              });

But with the second example, using jQuery the page loads forever
and never prints the p tags innerHTML values.

Why is this?

  • 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-23T02:19:39+00:00Added an answer on May 23, 2026 at 2:19 am

    calls to document.write after document.ready rewrites the document. and single the return value of document.getElementsByTagName is a live list the list becomes empty.

    So what actually happens is that the document contains.

    <p>Hello World!</p>
    <p>The DOM is very useful!</p>
    <p>This example demonstrates the <b>length</b> property.</p>
    

    When the document is ready you call x = document.getElementsByTagName("p"); and x contains an array of <p> elements. But this is a live list and reflects the live state of the document. So when <p> elements get removed the list gets updated automatically.

    You then call document.write with "-----<br/>" and this empties the document and replaces it with that. So the variable x now contains 0 <p> elements becuase there are 0 in the document.

    The for loop doesn’t run because x.length === 0

    As mentioned this is broken because W3Schools gives you broken code.

    I highly recommend you learn from the MDN

    Live Example

    A better way to achieve this with jQuery would be :

    $(document).ready(function() {
    
        var ps = $("p");
        var body = $(document.body);
    
        body.append("------" + "<br/">);
        ps.each(function() {
           body.append(this.innerHTML + "<br/>"); 
        }):
        body.append("------");
    
    });
    

    Live Example

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

Sidebar

Related Questions

I reading articles about XSD on w3schools and here many examples. For example this:
Reading the Scala by Example book and there is this example when Martin explains
Reading this site, I've found this : [The] line private static final Foo INSTANCE
Reading Real World Haskell, on page 95 the author provides an example: myFoldl f
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading PHP & jQuery Cookbook right now and it gives this example for using
Reading this question I found this as (note the quotation marks) code to solve
just reading the W3schools HTML DOM tutorial. There's a paragraph that makes no sense
Reading the code of the C library's sockets interface, I found this: /* Types
reading the SCJP book, I've found something like this in the chapter 1 self-test

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.