Iterate through all divs within a div

Hi there,
I need to iterate through a Div which contains other divs.
I want to find all the ones with ID that starts with "ReportDataDivID".  The following code works, but I am iterating via all the DOM.  I want to just go over the testDiv which will be passed to this function. How can I efficiently do this.  Thank you.

var __ensureVisible = function(testDiv){
        var divs = document.getElementsByTagName("div");
        for(var i = 0; i < divs.length; i++){
            var regexp = new RegExp('^' + "ReportDataDivID");
                $("#" + divs[i].id).show();

Open in new window

Who is Participating?

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

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.

Tom BeckCommented:
Assuming that "testDiv" holds the ID of the testDiv then you can do this:

var divs = document.getElementById(testDiv).getElementsByTagName("div");

Or if "testDiv" IS the element then:

var divs = testDiv.getElementsByTagName("div");
Rainer JeschorCommented:
you have added jQuery Topic - so in case you can use jQuery then the following should work:
$("div[id^='ReportDataDivID']", $("#testDiv")).show();

Open in new window

The first selector selects all divs where the ID starts with ReportDataDivID. The second parameter sets the scope where you can also use a jQuery selector.

Live sample:


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
ambuliAuthor Commented:
Thank you very much for your answer.  It works great.  Can you please let me know where can I see the documentation for the jquery select API.  Thank you.
Rainer JeschorCommented:

the main jQuery API is here:

Further selector choices:

Thanks for the points
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

From novice to tech pro — start learning today.