Delete only the children divs from a parent div

Hi, I'm trying to delete all the children divs that I created dynamically in a parent div. The parent div contains different controls, not just divs.

 var imgContainer = document.getElementById("ContentPlaceHolder1_div_Container");
    var childDivs = imgContainer.getElementsByTagName("div");

    var NumberOfDivs = childDivs.length;

    for (i = 0; i < NumberOfDivs; i++) {
        imgContainer.removeChild(childDivs[i]);
    }

Open in new window


If there is only one child div, the code works fine. However, when I append multiple divs I get the following error

"JavaScript runtime error: No such interface supported"

Any ideas?

Thanks
Adrian CrossAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
when you remove the first one, the index of the second become the first one
so replace your"for" loop by this a while like this :
        while(imgContainer.getElementsByTagName("div").length > 0) {
            imgContainer.removeChild(childDivs[0]);
        }

Open in new window


        var imgContainer = document.getElementById("ContentPlaceHolder1_div_Container");
        while(imgContainer.getElementsByTagName("div").length) {
            imgContainer.removeChild(childDivs[0]);
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Adrian CrossAuthor Commented:
Excellent! Many thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.