Theme: Light bulbs & switches
It seemed most reasonable to base all of the demos around a common theme. In this way, the reader can focus attention on the new material rather then trying to comprehend the problem.
Each sample will examine a representation of a light bulb and a light switch. The overall idea is that the light switch will turn the bulb ON and OFF. This should be a concept that is quite understandable.
You should examine the source code for each of the examples to see the whole file containing all of the details. Major portions will be presented here. The versions of the file were constructed as follows:
First, you must make sure that you understand the problem. In this case, we have 2 objects in our world, a light bulb and a light switch. The bulb has 2 possible states, ON or OFF. Furthermore, the switch controls the transitions between these states.
In most cases, a button will be used as the representation of the switch. We will assume, except where noted, that each switch controls exactly 1 bulb. The light bulb will first be represented as a text box, then as a check box, and finally as an image.
<HTML> <HEAD> <TITLE> Demo 10/9/98 </TITLE> <SCRIPT LANGUAGE="JAVASCRIPT"> </SCRIPT> </HEAD> <BODY> <h2> A light switch </h2> <form NAME="lights"> </form> </body> </html>
<form NAME="lights"> <b>Light</b><br> <input TYPE=text NAME="bulb" VALUE="Off" SIZE=5> <p> <input TYPE="button" NAME="switch" VALUE="switch"> </form>
<script language="javascript"> var light_state; light_state=0; </script>and the form is now modified so that the button can handle a click event.
<form NAME="light"> <b>Light</b><br> <input TYPE=text NAME="bulb" VALUE="Off" SIZE=5> <p> <input TYPE="button" NAME="switch" VALUE="switch" onclick=' if (light_state == 0) { light_state = 1; light.bulb.value="On"; } else { light_state = 0; light.bulb.value="Off"; } '> </form>
<form NAME="light"> <b>Light</b><br> <input TYPE="checkbox" NAME="bulb" > <p> <input TYPE="button" NAME="switch" VALUE="switch" > </form>
<input TYPE="checkbox" NAME="bulb" > <p > <input TYPE="button" NAME="switch" VALUE="switch" onclick=' if (light.bulb.checked) light.bulb.checked = false; else light.bulb.checked = true; '>
<input TYPE="button" NAME="switch" VALUE="switch" onclick=' light.bulb.checked = !( light.bulb.checked) ; '>
<script language="javascript"> function flip() { if (document.light.bulb.checked) document.light.bulb.checked = false; else document.light.bulb.checked = true; } </script>and the event handler, which will call the function looks like
<input TYPE="button" NAME="switch" VALUE="switch" onclick='flip()'>
<input TYPE="checkbox" NAME="bulb1" > <input TYPE="checkbox" NAME="bulb2" > <input TYPE="button" NAME="switch1" VALUE="switch" onclick=' if (document.light.bulb1.checked) document.light.bulb1.checked = false; else document.light.bulb1.checked = true; '> <input TYPE="button" NAME="switch2" VALUE="switch" onclick=' if (document.light.bulb2.checked) document.light.bulb2.checked = false; else document.light.bulb2.checked = true; '>
The button definitions with the function calls
<form NAME="light"> <input TYPE="checkbox" NAME="bulb1" > <input TYPE="checkbox" NAME="bulb2" > <input TYPE="button" NAME="switch1" VALUE="B 1" onclick=' flip(document.light.bulb1); '> <input TYPE="button" NAME="switch2" VALUE="B 2" onclick=' flip(document.light.bulb2); '>
Now, for the function definition, note the declaration of the parameter and the usage of the parameter within the body.
<script language="javascript"> function flip(bulb) { if (bulb.checked) bulb.checked = false; else bulb.checked = true; } </script>
The switch behavior is as in the previous example. The only changes are to the bulbs, not the switches.
<input TYPE="checkbox" NAME="bulb1" checked onclick="return false" > <input TYPE="checkbox" NAME="bulb2" onclick="return false" >however, that method did not alway work and the alternative below, seemed to work on my particular machine. Try it
<input TYPE="checkbox" NAME="bulb1" checked onclick="bulb1.checked = !bulb1.checked" > <input TYPE="checkbox" NAME="bulb2" onclick="bulb2.checked = !bulb2.checked" >
The button definition
<input TYPE=button VALUE="Report Bulbs" onclick=' report_bulbs(0, 4); ' >The function definition is a little complicated the first time you see it.
function report_bulbs( bulb_num, howmany) { var repstr, curstr, i, bstat; repstr = ""; for ( i = 0; i < howmany ; ++i) { bstat = document.light.elements[bulb_num+i].checked; if (bstat) curstr = "Bulb "+i+" is ON" ; else curstr = "Bulb "+i+" is OFF" ; repstr = repstr + curstr + "\n" } alert(repstr); }
The final version Try it