Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    44
    Thanks
    11
    Thanked 0 Times in 0 Posts

    passing parameter in window.onload function

    Code:
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    window.onload = function(){h = parseInt(document.getElementById("div1").style.height);}
    
    var t;
    
    function addHeight(openAccordionID){
    	clearInterval(t);
    	document.getElementById(openAccordionID).style.height = h+"px";
    	h+=1;
    	t = setInterval("addHeight('"+openAccordionID+"')",10);
    	for(var x=1; x<h; x++)
    	if(h%(x*100)==0){clearInterval(t);}
    	}
    
    </script>
    
    </head>
    <body>
    
    <div id="div1" onclick="addHeight(this.id)" style="width: 200px; height: 20px; background-color: yellow;">Increase Height</div>
    
    </body>
    </html>
    How can I still use the parameter "openAccordionID" in the blue-highlighted part? I tried, but it failed.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    Why would you want to do that?

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    44
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    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.

  • #5
    New Coder
    Join Date
    Apr 2011
    Posts
    44
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I see. Thanks. If JS does not allow this, I will not search the solution and spare the time on other stuff of JS.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •