1

I'm trying to make a function in javascript and pass in the parameter "name", then when the user clicks on a photo, an alert will say something like "this photo was taken in ____"

<img src="photos/PhotoVersailles.jpg" onclick="photoWhere(Versailles)" style="width:100%">

<script type="text/javascript">
function photoWhere(name)
    {
        alert("This photo was taken in "+name+".");
    }
</script>

Why isn't my attempt working?

5
  • 1
    onclick="photoWhere(Versailles)" should be: onclick="photoWhere('Versailles')" Commented Dec 12, 2017 at 5:42
  • onclick="photoWhere('Versailles')" you forgot to wrap Versailles in quotes. Commented Dec 12, 2017 at 5:42
  • You need to actually ask a question if you want an answer. Commented Dec 12, 2017 at 5:43
  • What is the Question.? Commented Dec 12, 2017 at 5:44
  • The question is why isn't his or her attempt working. Commented Dec 12, 2017 at 5:44

3 Answers 3

2

Try 'Versailles' when passing as parameter

function photoWhere(name) {
  alert("This photo was taken in " + name + ".");
}
<img src="photos/PhotoVersailles.jpg" onclick="photoWhere('Versailles')" alt="not found" style="width:100%">

Sign up to request clarification or add additional context in comments.

1 Comment

You have to see it uses " ' ' " or ' " " '
0

Syntax is wrong, change your alert to:

var nameStr = "This photo was taken in "+name+".";
alert(nameStr);

Comments

0

you can use this:

function photoWhere(Pic) { var _pic = Pic.name; _pic = "This photo was taken in "+_pic+"." alert(_pic); }
<img src="photos/PhotoVersailles.jpg" onclick="photoWhere(this)" 
name="Versailles" style="width:50%">

1 Comment

Welcome to SO! When you post an answer that is mostly code, please explain it a little bit.

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.