how to run collapse on pageload

Maybe a simple javascript question but I couldn't solve it. I want the javascript code collapse Main title when the page loads.

How?
thank you...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function switchMenu(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != "none" ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}
function collapseAll(objs) {
	var i;
	for (i=0;i<objs.length;i++ ) {
		objs[i].style.display = 'none';
	}
}
function pageLoad() {
	collapseAll($('mydiv1'));
}
addEvent(window,'load',pageLoad);
//-->
</script>
 
</head>
 
<body>
		<h1><a onclick="switchMenu('mydiv1');" title="Main title">Main title</a></h1>
		<div id="mydiv1">
		<h2>subtitle</h2>
		<p align="justify">test123
</p>
	</div>
 
</body>
</html>

Open in new window

cemlouisAsked:
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.

MMDeveloperCommented:
I'd change

addEvent(window,'load',pageLoad);

to

window.onload = function() {
      pageLoad();
};
0
hieloCommented:
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function $(str){return document.getElementById(str);}
function switchMenu(obj) {
      var el = document.getElementById(obj);
      if ( el.style.display != "none" ) {
            el.style.display = 'none';
      }
      else {
            el.style.display = '';
      }
}
function collapseAll(objs) {
      var i;
      for (i=0;i<objs.length;i++ ) {
            objs[i].style.display = 'none';
      }
}
function pageLoad() {
      collapseAll( [$('mydiv1')]);
}
window.onload=pageLoad;
//-->
</script>
 
</head>
 
<body>
            <h1><a onclick="switchMenu('mydiv1');" title="Main title">Main title</a></h1>
            <div id="mydiv1">
            <h2>subtitle</h2>
            <p align="justify">test123
</p>
      </div>
 
</body>
</html>

Open in new window

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
cemlouisAuthor Commented:
hielo,

Your solution works but what if there is more than 1 divs? I tried below but not working:

function pageLoad() {
      collapseAll( [$('mydiv1', 'mydiv2')]);
}

Any solutions?
0
hieloCommented:
try:
function pageLoad() {
      collapseAll( [$('mydiv1'), $('mydiv2'), $('mydiv3') ] );
}

Open in new window

0
cemlouisAuthor Commented:
thanks...
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.