# Solved: Add onclick to dynamically generated <img> tag



## andynic (May 25, 2007)

Hi,

The code shown below works just as I want it to except for the "onclick". Is there a way to do this?

"This" = do everything the code does after the user clicks on the "click" button, but when the user in turn clicks on the image, the alert "hello" displays. (Actually I want to pop up a larger version of the image, but I'd be satisfied to get the "hello" for now.)

Thanks,
Andynic


<img name="imgNm" />
</table -->


----------



## andythepandy (Jul 2, 2006)

Instead of putting what you want to happen on the onclick event in speech marks, you need to write it as a function like this:


```
document.imgNm.onclick=function(){alert("hello");}
```
Andy


----------



## andynic (May 25, 2007)

Thanks, Andy. Will give it a try next week when I am back home. Will let you know.
Andynic


----------



## andynic (May 25, 2007)

Yes, that works.
Thanks very much!


----------

