View Full Version : passing parameter in window.onload function

08-18-2011, 01:39 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
window.onload = function(){h = parseInt(document.getElementById("div1").style.height);}

var t;

function addHeight(openAccordionID){
document.getElementById(openAccordionID).style.height = h+"px";
t = setInterval("addHeight('"+openAccordionID+"')",10);
for(var x=1; x<h; x++)



<div id="div1" onclick="addHeight(this.id)" style="width: 200px; height: 20px; background-color: yellow;">Increase Height</div>


How can I still use the parameter "openAccordionID" in the blue-highlighted part? I tried, but it failed.

08-18-2011, 02:11 PM
Why would you want to do that?

08-18-2011, 03:44 PM
Why would you want to do that?

Just to make sure that whenever I use this script as a template, I only need to change the parameter in the JS part. Need not to go to HTML and JS to change both of the entries for the element ID.

08-18-2011, 04:31 PM
There is no way around this other than keeping it "div1". As soon as you change it in HTML you need to change it in Javascript too. Otherwise window.onload will not know about the element you are referring to

Other than your onclick it has not been initiated by the element.

08-18-2011, 04:47 PM
I see. Thanks. If JS does not allow this, I will not search the solution and spare the time on other stuff of JS.