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 8705349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:20:21+00:00 2026-06-13T03:20:21+00:00

In my HTML I have a content div that contains articles created by a

  • 0

In my HTML I have a content div that contains articles created by a php script and handled with JQuery and Javascript. With Javascript I collect those articles into an array with the ‘getElementsByClassName’ function.
The scripts and HTML I’m writing here are simplified. I need this array because I’m trying to make that if the length of the array is larger than 10, only 10 articles are displayed per page. So page 1 will be index 0 to 9 of the array, page 2 index 10 to 19, etc.
Now,

Take this html code.

<body>
<div id="content">
  <div class="article">
    <p>Article 1</p>
  </div>
  <div class="article">
    <p>Article 2</p>
  </div>
  <div class="article">
    <p>Article 3</p>
  </div>
</content>
</body>

And this Javascript code.

$.post("getarticles.php",{ page:"home" } ,function(data){
        //place the content
        document.getElementById("content").innerHTML = jQuery.trim(data);

        //put elements in array
        arrArticles = document.getElementsByClassName("article");
        alert(arrArticles.length);

        document.getElementById("content").innerHTML = "";
        alert(arrArticles.length);
})

The first alert gives me “3”, which is correct.. But the second alert gives me “0”. Why is the array losing it’s elements after the elements have been put in it?

By the way, the ‘data’ variable is a string of HTML passed on by a php script.
For example: In the php script I have the code

echo "<div class="article"><p>Article 1</p></div><div class="article"><p>Article 2</p></div><div class="article"><p>Article 3</p></div>"

&

document.getElementById("contentWrap").innerHTML = "";

I know this is causing the problem, but I don’t know why. Can anyone explain or provide an alternative?

Thanks in advance

  • 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-06-13T03:20:22+00:00Added an answer on June 13, 2026 at 3:20 am

    It looks like document.getElementByClassName is returning a reference to the element array which is being updated when you change the mark up. Not sure why; perhaps somebody better informed than me can answer that.

    Using jQuery to select the array worked for me:

    var arrArticles = $(".article")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Iframe that contains HTML content with a div assigned to every
I have a html that contains below content: <div class=ui-btn-text> <a id=12323 class=listviewLines ui-link-inherit
I want to parse a html content that have something like this: <div id=sometext>Lorem<br>
I have some HTML that looks like this: <div class=product> Content In Here <div
I have an html page which contains a div that displays the html from
I have a section of html that looks like this: <div id=faq-content> <div class=faq-topic
I have a content div, that has a header div, which contains two other
In HTML, I have a div that contains a table of labels and textboxes
Basically i have a div on my main page that contains the content, now
A jquery question. I have a div that contains text and an anchor tag.

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.