0

I want to use the same string in my img "src" in my "onclick" as a parameter to a function. What's the easiest way to do this? It works fine if I type it out both times but it doesn't look nice.

Here is an example so you can see what I mean...

<img src="/images/image1.jpg" onclick="doStuff('/images/image1.jpg')"/>

I'm wondering if there is a simple way to have the src in the onclick too that doesn't require me to write it out again? So I can use a general onclick for each image that needs to be clicked.

3 Answers 3

3
<img src="/images/image1.jpg" onclick="doStuff(this.src)"/>
Sign up to request clarification or add additional context in comments.

Comments

2
doStuff(this.src);

1 Comment

Thank you. These answers are all correct, how do I choose the one to vote up and which to accept as the answer?
1

The easiest thing is to pass the object itself into the function. So:

<img src="/images/image1.jpg" onclick="doStuff(this.src)">

Will do what you want, I believe.

1 Comment

I have chosen this as the correct answer because although they are all correct, this one is the most personal and Jamie took the time to write complete sentences. I do Like efficiency but I am also a human being. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.