Page 1 of 1

Call JavaScript function with parameter in HTML tag

Posted: 06 Apr 2015, 14:49
by PloMar
Hi,
I have the following code:

Code: Select all

<img src="image.png" alt="Image" onclick="test(data)" />

<script>
function test(prm) {
  alert(prm)
  console.log(prm);
}
</script>
When I click on the image, the alert() is not executed, and I get this error in console:

Code: Select all

ReferenceError: data is not defined
How to add the "onclick" in the img tag so the test() function alerts the value "data"?

Call JavaScript function with parameter in HTML tag

Posted: 06 Apr 2015, 14:55
by Admin
Hi,
If the argument is a string, you should add it between quotes, like this:

Code: Select all

<img src="image.png" alt="Image" onclick="test('data')" />