/    Sign up×
Community /Pin to ProfileBookmark

How to get src value of image clicked on

I’m sure I’ve done this before, but I can’t find where it is… I have a photo album with images whose names end with _Thumb. I then have those same images in the full size without the _Thumb. What I want to do is to have a link on the _Thumb images to call a Javascript. That function would then take the source of the image that was clicked and strip out the _Thumb to get the full size image name. How do I code the javascript to get the src of the selected image?

I’ve tried using this.src in the function, but I get the value “undefined”. I also tried using an onclick event instead of a normal a href, and it still doesn’t work.

Chris

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 14.2014 — Show some code that you have attempted.

Perhaps it works but you have a syntax or typo error.

Make it brief, if possible.
Copy linkTweet thisAlerts:
@chrscoteauthorFeb 14.2014 — I just figured out my problem. I had forgotten to use event.srcElement. Instead, I was just using this.src. So now I have:

function changeImg() {

var clickedImg = event.srcElement.src;

var mainImg = document.getElementById("mainImg");

var newImg = clickedImg.substr(0, clickedImg.indexOf(".")-6); //Take of _Thumb.jpg

mainImg.src = newImg+".jpg";

}
Copy linkTweet thisAlerts:
@aubenefitFeb 15.2014 — you can use Jquery and get many values from object.

If get value of src, you can use attribute "attr"

See example on this:

http://api.jquery.com/attr/

Thanks!
×

Success!

Help @chrscote 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.18,
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,
)...