...

View Full Version : Resolved Help with running an Onload function?



T0Rtur3
05-06-2009, 06:35 PM
Hello. I am new here. I am trying to run this javascript function:



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

adios
05-06-2009, 07:40 PM
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.'

T0Rtur3
05-06-2009, 08:04 PM
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!

venegal
05-06-2009, 08:12 PM
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.

adios
05-06-2009, 08:18 PM
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 ...

T0Rtur3
05-06-2009, 08:23 PM
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:

echo"<SCRIPT LANGUAGE=\"JavaScript\">
var sElement = ".$i.";
</SCRIPT>\n";

Then my javascript looks like this(the idElement2 is used for another function later on):

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum