Simple DHTML question

Hey,

I wrote this script:


function kliklamp(){
licht = document.getElementById("img_licht");
licht_aan = licht.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_aan.gif";
licht_uit = licht.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_uit.gif";
schakelaar = document.getElementById("img_lichtschklr");
schakelaar_aan = schakelaar.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_aan.gif";
schakelaar_uit = schakelaar.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_uit.gif";
if(schakelaar.src == schakelaar_aan){
	schakelaar.src = schakelaar_uit;
	licht.src = licht_uit;
	document.body.style.backgroundColor = "#000000";
	}
else{
	schakelaar.src = schakelaar_aan;
	licht.src = licht_aan;
	document.body.style.backgroundColor = "#FFFFFF";
	}
}

Basically what it should do is when an image is clicked images need to be swapped and the background needs to change color. The script doesn’t work but it doesn’t throw any errors either. Anyone got an idea why it does not work?

Are you sure the src values are correct? They look a bit odd with those dots and spaces in them.

Also, this:

if(schakelaar.src == schakelaar_aan)

is always going to be false, because you’re setting schakelaar.src to the “uit” image here (second line):

schakelaar_aan = schakelaar.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_aan.gif";
schakelaar_uit = schakelaar.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_uit.gif";

Same with the licht images. You’re setting it to the aan image first, then immediately to the uit image. Why?

Ah you found the error. The paths are correct but the assignment to the different variables is not.

What I want to do is have a var pointing to an image via .src attribute.
I want the schakelaar_aan to contain the image of the on-switch and schakelaar_off to contain the image of the off-switch. Same for the light.

Apparently you can’t store an attribute value into a variable?
and ill have to take me refuge to


if(schakelaar.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_uit.gif";
 == schakelaar.src = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_aan.gif";
){
...
}

or how can I adress these images?

You seem to be getting into a knot. Try this:

function kliklamp(){
var licht = document.getElementById("img_licht"),
  schakelaar = document.getElementById("img_lichtschklr"),
  licht_aan = "1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_aan.gif",
  licht_uit = "1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_uit.gif",
  schakelaar_aan = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_aan.gif",
  schakelaar_uit = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_uit.gif";
if (schakelaar.src == schakelaar_aan){
    schakelaar.src = schakelaar_uit;
    licht.src = licht_uit;
    document.body.style.backgroundColor = "#000000";
    }
else{
    schakelaar.src = schakelaar_aan;
    licht.src = licht_aan;
    document.body.style.backgroundColor = "#FFFFFF";
    }
}

You didn’t need the “= licht.src =” in the middle, just “=”. You were assigning the new src to the actual image by doing that. And yes, you can assign the value of an attribute to a variable, but there is no point as you are going to be comparing it and then changing it immediately afterwards.

Also note how I used the “var” statement. That is to avoid the use of global variables, which are a Bad Thing.

Aha, fantastic, I learned alot!

Thank you!

But wait a second, hm, the script still doesn’t run. Although everything you have said seemed logic and correct. It still does not change the image when it is clicked on.
The paths are 100% correct since the images display so that is one thing we can exclude.

What else remains?

The starting scenario displays with an image == schakelaar_aan. So this should execute upon clicking or am I wrong?


if (schakelaar.src == schakelaar_aan){
    schakelaar.src = schakelaar_uit;
    licht.src = licht_uit;
    document.body.style.backgroundColor = "#000000";
    }

I have included the whole html page so you can have a complete look:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lichtschakelaar - DHTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Lichtschakelaar.css" />
<script type="text/javascript">
/* <![CDATA[ */
function kliklamp(){
var licht = document.getElementById("img_licht");
var schakelaar = document.getElementById("img_lichtschklr");
var licht_aan = "1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_aan.gif";
var licht_uit = "1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_uit.gif";
var schakelaar_aan = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_aan.gif";
var schakelaar_uit = "1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_uit.gif";
if (schakelaar.src == schakelaar_aan){
    schakelaar.src = schakelaar_uit;
    licht.src = licht_uit;
    document.body.style.backgroundColor = "#000000";
    }
else{
    schakelaar.src = schakelaar_aan;
    licht.src = licht_aan;
    document.body.style.backgroundColor = "#FFFFFF";
    }
}
/* ]]> */
</script>
</head>
<body>
<div id="wrapper">
	<div>
		<img src="1. Oef/11. Lichtschakelaar/Afbeeldingen/licht_aan.gif" id="img_licht" alt="licht" />
	</div>
	<div>
		<img src="1. Oef/11. Lichtschakelaar/Afbeeldingen/schakelaar_aan.gif" id="img_lichtschklr" alt="lichtschakelaar" onclick="kliklamp()" />
	</div>
	<div>
		<p>
			Kleertjes uit, pyamaatjes aan,<br />
			hoogste tijd om naar bed toe te gaan.<br />
			Kijk eens even op de klok,<br />
			alle kippetjes zijn al op stok.<br />
			Kom, we moeten nu slapen gaan,<br />
			kleertjes uit en pyamaatjes aan.
		</p>
	</div>
</div>
</body>
</html>

P.S: don’t mind the poem :lol:

I just copied over that code, saved it and ran it. Clicking the second image changed the images (I used some local images) and made the background black. So it seems to be working.

That is weird. Doesn’t run in any of the browsers I tested(FF3.6,IE8,Safari).
You didn’t adapt anything else besides the images?

instead of checking for images I tried adding a variable which can be switched between true or false


[B]var lichtaan = true;[/B]
function kliklamp(){
var licht = document.getElementById("img_licht");
var schakelaar = document.getElementById("img_lichtschklr");
var licht_aan = "licht_aan.gif";
var licht_uit = "licht_uit.gif";
var schakelaar_aan = "schakelaar_aan.gif";
var schakelaar_uit = "schakelaar_uit.gif";
if ([B]lichtaan[/B]){
    schakelaar.src = schakelaar_uit;
    licht.src = licht_uit;
    document.body.style.backgroundColor = "#000000";
    [B]lichtaan = false;[/B]
    }
else{
    schakelaar.src = schakelaar_aan;
    licht.src = licht_aan;
    document.body.style.backgroundColor = "#FFFFFF";
    [B]lichtaan = true;[/B]
    }
}

Now it seems to work. You must have some magic fairies in your computer making the previous version work :wink:

Yes, I have the magic fairies addon for Firefox. You should install it!

Lying makes your nose grow longer (:

So that’s what it was!

It’s ok :wink:

I wouldn’t do it too much though if I were you. Your credibility could sink fast.

On another note. :slight_smile:

Do you have any idea why comparing the src attribute doesn’t work?

No, but I’ll show you exactly what I used.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lichtschakelaar - DHTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Lichtschakelaar.css" />
<script type="text/javascript">
/* <![CDATA[ */
function kliklamp(){
var licht = document.getElementById("img_licht");
var schakelaar = document.getElementById("img_lichtschklr");
var licht_aan = "http://www.tes.co.uk/magazine/graphics/logo.png";
var licht_uit = "http://www.tes.co.uk/tesassets/images/calculator.png";
var schakelaar_aan = "http://www.tes.co.uk/magazine/graphics/logo.png";
var schakelaar_uit = "http://www.tes.co.uk/tesassets/images/calculator.png";
if (schakelaar.src == schakelaar_aan){
    schakelaar.src = schakelaar_uit;
    licht.src = licht_uit;
    document.body.style.backgroundColor = "#000000";
    }
else{
    schakelaar.src = schakelaar_aan;
    licht.src = licht_aan;
    document.body.style.backgroundColor = "#FFFFFF";
    }
}
/* ]]> */
</script>
</head>
<body>
<div id="wrapper">
	<div>
		<img src="http://www.tes.co.uk/magazine/graphics/logo.png" id="img_licht" alt="licht" />

	</div>
	<div>
		<img src="http://www.tes.co.uk/magazine/graphics/logo.png" id="img_lichtschklr" alt="lichtschakelaar" onclick="kliklamp()" />
	</div>
	<div>
		<p>
			Kleertjes uit, pyamaatjes aan,<br />
			hoogste tijd om naar bed toe te gaan.<br />

			Kijk eens even op de klok,<br />
			alle kippetjes zijn al op stok.<br />
			Kom, we moeten nu slapen gaan,<br />
			kleertjes uit en pyamaatjes aan.
		</p>
	</div>
</div>
</body>
</html>

I’m using FF3.6. If it doesn’t work with your images, you need to debug it by yourself. The simplest way to do this is to put an alert() at each point you think it might be failing, e.g. just before and just after the src comparison.

So you weren’t lying? Ha you are confusing me, evil one! :lol:

Ok I will look into it, thank you for the tip :wink:

well it definitely goes wrong here:


if (schakelaar.src == schakelaar_aan){

but why? The images all display so it can’t be a faulty path. I don’t see any other option which could lead to an error. It’s not that I don’t want to search myself but I just don’t see anything else that could be wrong.

Ah but you don’t either so ok it will stay a mistery forever.

Edit: Ah yes nevermind I got it now. It’s the paths :lol: