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...
How to check which image/link is clicked?
Moderator: General Moderators
-
galligraph
- Forum Newbie
- Posts: 11
- Joined: Wed Dec 21, 2005 10:26 am
-
galligraph
- Forum Newbie
- Posts: 11
- Joined: Wed Dec 21, 2005 10:26 am
feyd | Please use
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]
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]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.
If you want the hardest, but with Ajax and more control see this:
http://www.webdeveloper.com/forum/showt ... p?t=135040
http://www.webdeveloper.com/forum/showt ... p?t=135040