Expanding / Collapse specific div tags

T

Tim

hi all,

Im just starting to get into javascript, and what I am trying to do is
find a script that I can run on page load that will collapse / hide all
div tages that have an id that begins with "div_".

Basically I have a page with an infinite amount of div tags each having
their own id, ie div_0 , div_1 etc etc ..

on page load i want them all closed, but only these ones . not any
other div on the page .. how would I be able to do this ..

any help greatly appreciated ..

thanks

tim
 
I

Ivo

Tim said:
hi all,

Im just starting to get into javascript, and what I am trying to do is
find a script that I can run on page load that will collapse / hide all
div tages that have an id that begins with "div_".

Basically I have a page with an infinite amount of div tags each having
their own id, ie div_0 , div_1 etc etc ..

on page load i want them all closed, but only these ones . not any
other div on the page .. how would I be able to do this ..

any help greatly appreciated ..

thanks

tim

Put all div elements in an array and loop through that array, checking every
element's id as you go. For example like this:

function hidedivs() {
var els = document.getElementsByTagName( 'div' );
var num = els.length;
for( var i = 0; i<num; i++ ) {
if( els.id.indexOf( 'div_' )===0 ) {
els.style.display = 'none';
}
}
}
window.onload = hidedivs;

hth
ivo
http://4umi.com/web/javascript/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top