Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript onclick happening on page load

I'm trying to make a simple Javascript function to open a window with a bigger image when an image is clicked on. What happens is this test alert pops up on the page load and then does nothing when i click on the image. Here is the code

function zoom()
{
alert("test!");
}
document.getElementById("us").onclick=zoom();
like image 495
scalen121 Avatar asked Apr 28 '13 02:04

scalen121


3 Answers

Try this

function zoom()
{
    alert("test!");
}
document.getElementById("us").onclick=zoom;
like image 183
Tamil Selvan C Avatar answered Oct 07 '22 01:10

Tamil Selvan C


function zoom()
{
    alert("test!");
}
document.getElementById("us").onclick=function(){zoom(variable1)};

If you need to pass a variable.

like image 31
kravits88 Avatar answered Oct 06 '22 23:10

kravits88


for es6 onclick = () => zoom(i) works

like image 35
user2584621 Avatar answered Oct 06 '22 23:10

user2584621