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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with running an Onload function?

    Hello. I am new here. I am trying to run this javascript function:

    Code:
    <script language="javascript">
    function selectElement(idElement){
    var colorSelected='#FFFFCC';
    var colorNoSelected='#FFFFFF';
    divElement= document.getElementById('sel'+idElement);
    inputElement= document.getElementById('lselect'+idElement);
    
    if(inputElement.checked){
    divElement.style.background=colorSelected;
    }else{
    divElement.style.background=colorNoSelected;
    }
    }
    </script>
    So that it runs when the window loads. Meaning if a checkbox is already selected, the background will already be changed for it.

    Anyone have any ideas how to accomplish this?

    Thanks in advance.
    Last edited by T0Rtur3; 05-06-2009 at 08:38 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    window.onload = function()
    {
       selectElement(idElement);
    }
    You'll need to specify the idElement string, of course.
    Make sure you don't have any other 'onload' assignments, including <body onload ...>
    Generally known as 'keeping things in sync.'

  • Users who have thanked adios for this post:

    T0Rtur3 (05-06-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by adios View Post
    Code:
    window.onload = function()
    {
       selectElement(idElement);
    }
    You'll need to specify the idElement string, of course.
    Make sure you don't have any other 'onload' assignments, including <body onload ...>
    Generally known as 'keeping things in sync.'
    How would I specify the idElements if they are going to be dynamically selected by php (up to 2)? Sorry, I'm new to javascript.

    EDIT: nevermind I have it working now. Thanks!
    Last edited by T0Rtur3; 05-06-2009 at 08:13 PM.

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    From JS's POV, there's nothing dynamic about PHP. You could make PHP give the appropriate divElements a certain id or CSS class, so your JS can find them. Or you could just make PHP write your JS.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Lost me there. Are you trying to compensate for checkbox persistence - the fact that, when a user returns to the page, a box might be checked without the corresponding div styled? That's what I meant by 'keeping things in sync.' If that's the issue, some idea of how that page is coded would help, as you'll need to loop and check what's up ...

  • #6
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay, here is my solution. If anyone sees a problem with this, feel free to correct me.
    Using php I echo this javascript right after my last checkbox that was generated:
    PHP Code:
    echo"<SCRIPT LANGUAGE=\"JavaScript\">
    var sElement = "
    .$i.";
    </SCRIPT>\n"

    Then my javascript looks like this(the idElement2 is used for another function later on):
    Code:
    var idElement2;
    function selectElement(idElement){
    	var colorSelected='#FFFFCC';
    	var colorNoSelected2='#fefefe';
    	var colorNoSelected1='#e8e9ea';
    	idElement2 = idElement;
    	
    	divElement= document.getElementById('sel'+idElement);
    	
    	inputElement= document.getElementById('lselect'+idElement);
    	
    	if(inputElement.checked){
    	divElement.style.background=colorSelected;
    	}else{
    		if(idElement % 2 != 0){
    		divElement.style.background=colorNoSelected1;
    		}
    		else{
    		divElement.style.background=colorNoSelected2;
    		}
    	}
    }
    window.onload = function()
    {
    	for (i=1;i<=sElement;i++){
       			selectElement(i);
    	}
    }
    Thanks again for the help.


  •  

    Posting Permissions

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