/    Sign up×
Community /Pin to ProfileBookmark

Pass image name to function

I’m making a page with a simple photo gallery where you click a thumbnail then the larger image appears in a popup. I’ve found a couple of scripts to do that, but you have to explicitly type the path of the image to load into the href for each link. Is there a way to automatically detect and pass the name of the image that was clicked to a function?

Something like: javascript?opupPic(‘images/’+this.name), but not that cos it doesnt work :p

Basically its so i can put that same line into each link and not have to type a different path for each image

Cheers
Tim

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienFeb 08.2008 — In order to make your script web accessible, an <a> tag with the HREF pointing to the image is the recommended way to go:
[code=html]<a href="/images/path/to/photo.jpg" onclick="PopupPic(this.href); return false;"><img ... ></a>[/code]
It's accessible for one main reason. The image links are keyboard accessible. In your case, since you are linking to images and search engine optimization would probably do very little, you could try this markup:
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Image Popup Script</title>

<script type="text/javascript">
function PopupPic(btn) {
// Browser compatibility check
if (!document.getElementsByTagName) return;

var img = btn.getElementsByTagName("img")[0];

window.open(img.src, "_blank", "width="+img.clientWidth+", height="+img.clientHeight);
}
</script>

<style type="text/css" media="screen">
.imgPopup {
background: transparent none;
border-width: 0;
cursor: pointer;
display: inline;
margin: 0;
padding: 0;
}

.imgPopup img {
vertical-align: bottom;
}
</style>

</head>
<body>

<button type="button" class="imgPopup" onclick="PopupPic(this);"><img src="http://www.webdeveloper.com/forum/images/webdev-logo2.gif"></button>

</body>
</html>[/code]

The BUTTON element is keyboard accessible, though it relies entirely on JavaScript.
×

Success!

Help @timo22 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...