Forum Moderators: open

Message Too Old, No Replies

Multiple image swap functions.

Multiple flaggable items, currently works with one only

         

praxiz

8:11 am on Mar 7, 2007 (gmt 0)

10+ Year Member



Greetings, first post so be patient if i mess up some tags. :)

Ok, so I got this script that changes a flag from red to white.


<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
img1 = "images/white-flag.gif";
img2 = "images/red-flag.gif";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
}
</Script>
</head>
<body>
<img src="images/white-flag.gif" onClick="chng('img');" name="img" width="20" border="0" height="20">
</body>
</html>



As you can try for yourself, this script works fine.

It does, however, only work for one image (per page).

I have a list of about 150 items that each needs one flag next to it, and I need them all to be flaggable.

Ofcourse, I COULD just copy paste this code 150 times, but lets just say thats not going to happen.

So, can anyone help me rewrite this script so that it works with multiple images? I suppose they can have different name=""-tags, I can fix that in PHP, and dont worry about storing the "on or off"-value of the flag, Im taking care of that with another PHP-script.

regards, praxiz

cmarshall

10:58 pm on Mar 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't do the document[item] thing.

Try this:

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
img1 = "images/white-flag.gif";
img2 = "images/red-flag.gif";
function chng(c_img) {
if (c_img.src.indexOf(img1)!= -1) c_img.src = img2;
else c_img.src = img1;
}
</Script>
</head>
<body>
<img src="images/white-flag.gif" onClick="chng(this)" name="img" width="20" border="0" height="20">
</body>
</html>

cmarshall

11:30 am on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, yeah. Welcome to WebmasterWorld!

praxiz

8:01 am on Mar 9, 2007 (gmt 0)

10+ Year Member



Thank you! I didn't think it would be this easy.

But if you try it yourself, it does seem to be a bit .. "slow", I mean, if i click on a flag twice it might only change once, if i click too fast.

Any tips?

regards, praxiz.

cmarshall

11:23 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "slowness" is an illusion. Because of caching, the middle state is rendered and replaced in the blink of an eye. It may even not be shown at all. I'm no expert on this stuff (there are experts on it here, though).

You could add delays or disable the button during the event handler.

To wit (untested):

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
var g_disabled=false;
img1 = "images/white-flag.gif";
img2 = "images/red-flag.gif";
function chng(c_img) {
if (c_img.src.indexOf(img1)!= -1) c_img.src = img2;
else c_img.src = img1;
// You could add a delay here.
g_disabled=true;
}
</Script>
</head>
<body>
<img src="images/white-flag.gif" onClick="if(!g_disabled){g_disabled=true;chng(this)}" name="img" width="20" border="0" height="20">
</body>
</html>

cmarshall

10:13 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Duh.

// You could add a delay here.
g_disabled =[b]false[/b];