I have two javascript onloads in my form which seem to be cancelling each other out. The first- onload="checkMyBox"- works fine, which means the calendar stays hidden until the ckeckbox is clicked.However the second script that is supposed to show text ("I am an organized person") only after image is clicked does not work. It always shows the text.

I tried playing with it, and realized that if I erase the first <body onload="checkMyBox('c2','div1')"> the second onload works, meaning that the text will stay hidden until I click on image.

Does anybody know why is that and how I can fix it?Any help appreciated.Thank you.

My main concern is how you have structured your HTML as its 100% invalid, HTML only allows for 1 <body> tag/element on the page. Below i have posted both your javascript and HTML so they are both valid and should work fine.

Thank you SgtLegend,I used your code and obviously it worked. But why do we add the checkMyBox('c2', 'div1') line of code to the window.onload = function?

I tried erasing that line of code and it obviously didn't work, but I don't understand why.

Oh, and thanks for the tip. I am extremely new at this so I didn't know html can only have one body tag.

chris_upjohn
—
2011-05-12T10:13:17Z —
#4

Your welcome

But why do we add the checkMyBox('c2', 'div1') line of code to the window.onload = function?

I simply followed the same code you had within your HTML

I tried erasing that line of code and it obviously didn't work, but I don't understand why.

To me it makes no sense that it wouldn't work without this line as its part of the checkbox onclick event. Is there a working demo of the page i can see?

adamschroeder
—
2011-05-12T17:24:24Z —
#5

I think I didn't express myself correctly. By asking you the checkMyBox question I was just trying to understand the javascript function and how it works. You know so I can learn from it and then write my own javascript.

When I erased part of the code you gave me (the CheckMyBox line), the text did not stay hidden. Again, I just did that so I could learn/understand what each line in the window.load function serves for.

chris_upjohn
—
2011-05-13T01:17:04Z —
#6

The basic concept of window.onload is that its for functions and methods that are needed when the DOM has fully loaded. Say for instance you try to select an element(s) on the page before it has fully loaded the DOM using document.getElementById() this would cause an undefined error and end the script where as window.onload waits for the DOM to fully load before executing the code inside the "anonymous" function.

If your still a little confused see the example below

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Javascript Example</title>
<script type="text/javascript">
//<![CDATA[
// This will NOT work due to the DOM not been fully
// loaded yet!
var ele = document.getElementById('myDiv');
alert(ele.innerHTML);
// This will work because the window.onload method waits
// for the DOM to be fully loaded
window.onload = function() {
var ele = document.getElementById('myDiv');
alert(ele.innerHTML);
};
//]]>
</script>
</head>
<body>
<div id="myDiv">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</body>
</html>

Paul_Wilkins
—
2011-05-13T04:19:01Z —
#7

SgtLegend said:

The basic concept of window.onload is that its for functions and methods that are needed when the DOM has fully loaded. Say for instance you try to select an element(s) on the page before it has fully loaded the DOM using document.getElementById() this would cause an undefined error and end the script

With the following code structure, you can select the element before the script, but not the element after it.

To select the div that comes after, you need to wait until the element becomes available. This can be achieved by:

using setTimeout or setInterval

putting a script after the element, such as at the end of the body

using an event to trigger a function, such as domready or onload

SgtLegend said:

where as window.onload waits for the DOM to fully load before executing the code inside the "anonymous" function.

The onload event also waits for images and everything else to load too, which can lead to quite a delay until the script runs. It's preferable to use the domisready event instead, as that fires when the DOM has loaded, but before much of the other slower loading content, such as images.

The point I want to make with this post though is this. Scripts don't need to wait for the complete DOM to finish loading. They can access anything that has loaded up to the point that the script is run.

chris_upjohn
—
2011-05-13T05:08:07Z —
#8

Thanks for the great explanation paul, i have in the past used a domisready event to load jQuery dynamically into a page and i agree that its a more powerful way to fire code just for the DOM.