Jump to content

Obtaining image filename from click.


bluebomber

Recommended Posts

By image link you mean something like this:<a href='#'><img src='image.jpg' /></a>?It is by all means possible to get the src of that image. You'd need to pass the anchor tag to your function, and then you could use getElementsByTagName or childNodes to get a reference to the image.

function getImgSrc(anchor) {   var image = anchor.getElementsByTagName('img')[0];   var imgSrc = image.src;      alert(imgSrc);   return false;}

With accompanying HTML:<a href='#' onclick='return getImgSrc(this);'><img src='image.jpg' /></a>The return statements are there to prevent the default action (following the link) from occurring.The use of the 'this' keyword passes a reference to the <a> tag that was clicked to the getImgSrc function.

Link to comment
Share on other sites

  • 2 weeks later...
Lots of ways. Here's one:The file path is a string. Use it's split function, passing "/" as the delimiter. The return value will be an array. The last element of the array is your plain filename.
I'm going to have to do quite a bit of reading before I understand this :)Update - Ok, I've just read about split functions and delimiters - I will give this a go - thanks.2nd Update - I've split the string and accessed the final entry in the array with stringsplit[stringsplit.length-1] - thanks!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...