...

View Full Version : script simplification help needed.



noPCtoday
07-19-2007, 06:46 AM
I wrote a code, what I want to achieve is that there are 4 images,
when the mouse is clicking down the image changes the picture, and when mouse up, it changes back.
and I wrote the longest junk ever:


<head>
<script>
</function imgChanger(argee,back)
{
if(!(back)){
if(argee=="a"){
document.getElementById(argee).setAttribute("src","a_down.png");}
else if(argee=="b"){
document.getElementById(argee).setAttribute("src","b_down.png");}
else if(argee=="c"){
document.getElementById(argee).setAttribute("src","c_down.png");}
else{
document.getElementById(argee).setAttribute("src","d_down.png");}
}
else
{
if(argee=="a"){
document.getElementById(argee).setAttribute("src","a.png");}
else if(argee=="b"){
document.getElementById(argee).setAttribute("src","b.png");}
else if(argee=="c"){
document.getElementById(argee).setAttribute("src","c.png");}
else{
document.getElementById(argee).setAttribute("src","d.png");}
}

}</script>
<head>
<body>
<img src="a.png" onmousedown="imgChanger('a');" onmouseup="imgChanger('a',true);" id="a"/>
<img src="b.png" onmousedown="imgChanger('b');" onmouseup="imgChanger('b',true);" id="b"/>
<img src="v.png" onmousedown="imgChanger('c');" onmouseup="imgChanger('c',true);" id="c"/>
<img src="d.png" onmousedown="imgChanger('d');" onmouseup="imgChanger('d',true);" id="d"/>
</body>

rnd me
07-19-2007, 07:30 AM
I wrote a code, what I want to achieve is that there are 4 images,
when the mouse is clicking down the image changes the picture, and when mouse up, it changes back.
and I wrote the longest junk ever:

i dont think it will get much shorter than this:



<head>
<script>

function imgChanger(argee,back) {
document.getElementById(argee).setAttribute("src", argee+(back?"_down.png":".png"))
}


</script>
<head>

<body>
<img src="a.png" onmousedown="imgChanger('a');" onmouseup="imgChanger('a',1);" id="a"/>
<img src="b.png" onmousedown="imgChanger('b');" onmouseup="imgChanger('b',1);" id="b"/>
<img src="v.png" onmousedown="imgChanger('c');" onmouseup="imgChanger('c',1);" id="c"/>
<img src="d.png" onmousedown="imgChanger('d');" onmouseup="imgChanger('d',1);" id="d"/>
</body>


/* alternate dom0 version, slightly more compact, slightly less compatible. */

function imgChanger(argee,back) {
document.getElementById(argee).src=argee+(back?"_down.png":".png")
}












got about 20 lines of code into 1 line, hope it helps.
cheers!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum