/    Sign up×
Community /Pin to ProfileBookmark

Cannot get image to work as link

Hello all,

With the following code I can get it to work when I use text for the link, however I cannot get it to work using an image.
Any suggestions?

The page I am working on is at [url]http://www.welcometothecommunity.com/PuebloWest/indexpueblo.html[/url]

[CODE]addEvent(window, ‘load’, et_init);

var et_swapElements = [];

/* Initialisation */
function et_init() {
var i, link, id, target, first;
first = true;
for (i = 0; (link = document.links[i]); i++) {
if (/bswapb/.exec(link.className)) {
id = link.href.split(‘#’)[1];
target = document.getElementById(id);
et_swapElements[et_swapElements.length] = target;
if (first) {
first = false;
} else {
target.style.display = ‘none’;
}
link.onclick = et_swap;
}
}
}

function et_swap(e) {

if (typeof e == ‘undefined’) {
var e = window.event;
}
var source;
if (typeof e.target != ‘undefined’) {
source = e.target;
} else if (typeof e.srcElement != ‘undefined’) {
source = e.srcElement;
} else {
return true;
}
/* For most browsers, targ would now be a link element; Safari however
returns a text node so we need to check the node type to make sure */
if (source.nodeType == 3) {
source = source.parentNode;
}
var id = source.href.split(‘#’)[1];
var elem;
for (var i = 0; (elem = et_swapElements[i]); i++) {
if (elem.id != id) {
elem.style.display = ‘none’;
} else {
elem.style.display = ‘block’;
}
}
return false;
}

function addEvent(obj, evType, fn){
if (obj.addEventListener) {
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent) {
var r = obj.attachEvent(“on”+evType, fn);
return r;
} else {
return false;
}
}[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 25.2006 — The event is from the image not the anchor.

[I]var id = source.href.split('#')[1];[/I] source here is the image when there is no text

Either check for [I]nodeName[/I] or have all images as a background
.
.
if(source.nodeName=='IMG') {source=source.parentNode;}
var id = source.href.split('#')[1];
.
.
×

Success!

Help @Rockynub spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...