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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:09:02+00:00 2026-05-27T08:09:02+00:00

I have a function that is meant to clear a div of all its

  • 0

I have a function that is meant to clear a div of all its children with a specific name before refilling said div with a new set of children.

function displaySearchResults(resultsList) {
var length = resultsList.length;
var searchDiv = document.getElementById('search-results');

//CLEAR DIV
if(searchDiv.hasChildNodes()) {
    for(var i=0; i<searchDiv.childNodes.length; i++) {
        var oldChild = searchDiv.childNodes[i];
        if(oldChild.id.indexOf('search-results-row') != -1) {
            searchDiv.removeChild(oldChild);
            console.log('Removed Result!');
        }
    }
}

//ADD CHILDREN  
for(var i=0; i<length; i++) {
    console.log('Added Result!');
    var element = resultsList[i];
    var rowDiv = document.createElement('div');
    rowDiv.id = 'search-results-row' + i;
    rowDiv.className = 'list listRow' + i%2;
    rowDiv.setAttribute('onclick', 'mapCurrentContact(' + i + ');');
    rowDiv.innerHTML = element.firstName + ' ' + element.lastName;
    searchDiv.appendChild(rowDiv);
}
}

However it starts running the ADD CHILDREN portion of the function before the CLEAR DIV loop has a chance to finish. So for instance, if the function finds 4 childNodes in the div and begins looping through to remove each one with the id of search-results-row, it will only get through 2 or so before it begins appending the new set of results that was initially passed into the function.

I know that Javascript runs asynchronously, but I thought that functions themselves at least run top down so the bottom portion should not do anything until we reach that part of the code? I have tried all sorts of other alternatives such as running the CLEAR DIV portion in its own function and then calling the ADD CHILDREN portion at the end, but I always get the same results. What am I missing here!?!

  • 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-27T08:09:03+00:00Added an answer on May 27, 2026 at 8:09 am

    When you remove items from the beginning of a list:

    V
    a b c d //Initial list with the counter being 0, pointing to 'a'
    
    V
    b c d   //Removed the element 'a'
    
      V
    b c d   //Incremented the counter by one
    

    You end up skipping over element b and repeating the same error for every other element in the list. You can either decrement the counter when you remove something or simply start from the end of the list, in which case you will only affect the index of elements you have already looked at. The latter is more widely accepted.

    var i; //JavaScript does not have block scope, only function scope.  Just declare it once here and initialize it at the beginning of each loop.
    for(i=searchDiv.childNodes.length-1; i>=0; i++) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside my Controller i have function that runs after user clicks on item, which
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that returns two values in a list. Both values need
I have a function that takes an input string and then runs the string
I have a function that does a long task and I want to update
I have a function that counts the amount of visits a page has, and
I have a function that I'm trying to optimize at the moment but I'm
I have a function that I want to execute on keyup for 2 different

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.