Reputation: 7
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?
Upvotes: 1
Views: 14180
Reputation: 7591
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%">
Upvotes: 2
Reputation: 1
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%">
Upvotes: 0
Reputation:
Syntax is wrong, change your alert to:
var nameStr = "This photo was taken in "+name+".";
alert(nameStr);
Upvotes: 0