Call JavaScript function with parameter in HTML tag
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
PloMar
- Posts:48
Call JavaScript function with parameter in HTML tag
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"?
Admin
Posts:805
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')" />
Similar Topics
- Hour and Minutes togheter in Javascript
JavaScript - jQuery - Ajax
First post
Dear Coursesweb I can not find out how to add the hours + minutes togheter.
<SCRIPT LANGUAGE= JavaScript >
day = new Date()
hr =...
Last post
See and use the following example:
<script>
var day = new Date();
let hr = day.getHours();
let mn = day.getMinutes();
let se =...
- Display message to every minute in Javascript
JavaScript - jQuery - Ajax
First post
Hello,
On eatch minute from the current hour I wanna have an message
I can not find out how to complete
I hope to get something like this (code...
Last post
If you only want to display a message to every minute, just use the setInterval() function. It calls a function repeatedly, over and over again, at...