Page 1 of 1

How to check which image/link is clicked?

Posted: Tue Jan 23, 2007 4:13 am
by galligraph
I got 7 links and 7 images. I need a function that would check which image is clicked and change it to another. Could someone help me the easiest way to do this??

Thx for the help...

Posted: Tue Jan 23, 2007 7:26 am
by feyd
The onclick event.

Posted: Tue Jan 23, 2007 12:34 pm
by galligraph
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I know that I should use Onclick event. So could someone help me a little bit more. I don't know exactly how to check which image is clicked and how to change it to another one. Here's my code... Thx for the answers.

[syntax="html"]
<a href="#" onclick="swap(this); content.load('news.html'); return false" onmouseover="setOver(0)" onmouseout="setOut(0)"><img src="images/out0.jpg" alt="news" name="news" width="71" height="25" border="0" id="Image0" /></a>

Code: Select all

function swap(obj) {
var imgs = document.getElementsByTagName('img');
var len = imgs.length;
for (var i=0; i<len; i++){
	
if (imgs[i].src.indexOf("out0.jpg") != -1) {
imgs[i].src = "images/over0.jpg";
}
if (imgs[i].src.indexOf("out1.jpg") != -1) {
imgs[i].src = "images/over1.jpg";
}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

I'm sorry

Posted: Wed Jan 24, 2007 9:18 pm
by a.heresey
I haven't the slightest what you're trying to do.

Is this an image rollon/ rolloff/ click function that you're making? If so, try looking into using the "this" keyword in your function.

Posted: Thu Jan 25, 2007 10:52 am
by pickle
So, in your example, when the link is clicked, you want the image (out0.jpg) to be be changed to something else? You could pass along the ID of the image (Image0) in your swap function instead of this. Then in your swap function, just act on the element with the ID that was passed.

Posted: Thu Jan 25, 2007 10:58 am
by nadavvin
If you want the hardest, but with Ajax and more control see this:
http://www.webdeveloper.com/forum/showt ... p?t=135040