How to check which image/link is clicked?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
galligraph
Forum Newbie
Posts: 11
Joined: Wed Dec 21, 2005 10:26 am

How to check which image/link is clicked?

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The onclick event.
galligraph
Forum Newbie
Posts: 11
Joined: Wed Dec 21, 2005 10:26 am

Post 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]
User avatar
a.heresey
Forum Commoner
Posts: 59
Joined: Wed Dec 13, 2006 7:31 pm
Location: Chesapeake, VA, US

I'm sorry

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post by nadavvin »

If you want the hardest, but with Ajax and more control see this:
http://www.webdeveloper.com/forum/showt ... p?t=135040
Post Reply