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");
            if(regexp.test(divs[i].id)){
                $("#" + divs[i].id).show();
            }
        }
    };

Open in new window

ambuliAsked:
Who is Participating?
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");
0
Rainer JeschorCommented:
Hi,
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:
https://jsfiddle.net/EE_RainerJ/f2k47bb2/

HTH
Rainer
0

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.
0
Rainer JeschorCommented:
Hi,

the main jQuery API is here:
http://api.jquery.com/

Further selector choices:
http://api.jquery.com/category/selectors/attribute-selectors/

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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.