Forum Moderators: open

Message Too Old, No Replies

Function problem

         

mooger35

10:42 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



Now I know this is easy and I'm missing something totally stupid but bare with me as I'm trying to teach myself this stuff... at least it's similar to PHP :-)

I'm trying to have have an image switch to another by clicking on it. And then when clicked again it will switch back.

Here's what i have so far:

<script language="javascript" type="text/javascript">
<!--
function switch(){
var id = document.getElementById("id").value;
var new_id = 1;
if(id == 1){
image_sm.src="image02.gif";
new_id = (id + 1);
}
if(id == 2) {
image_sm.src="image01.gif";
new_id = (id - 1);
}

document.getElementById("id").value = new_id;
}
//-->
</script>

and the call to the script:

<p><a href="#" onclick="javascript:switch();"><img name="image_sm" src="image01.gif" border="0" alt=""></a></p>
<input type="hidden" id="id" value="1">

mooger35

11:37 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



argh... nevermind. Switch must already be a function... a simple change to switch_img fixed my problem.

stuartc1

4:55 pm on Mar 26, 2008 (gmt 0)

10+ Year Member



Would this be easier using a CSS class to do the switch?

onMouseover="this.className='otherClass'"
onMouseout="this.className='originalClass'"

I guess it really depend on your situation.... just another idea that's all.

mooger35

5:20 pm on Mar 26, 2008 (gmt 0)

10+ Year Member



For the image switch, ya, you're probably right. I need it more for a hidden input value though. I took that part of the script out to simplify it.

mehh

8:32 pm on Mar 26, 2008 (gmt 0)

10+ Year Member



Switch must already be a function...

Its a statment
var someVar="foo"
switch(someVar){
case "foo":
case "bar":
alert("someVar = foo or bar");
break;
case "other":
alert("someVar = other");
break;
default:
alert("What?");
}

more information here [w3schools.com]