makaroN
makaroN

Reputation: 315

How to execute javascript function when the user clicks on the image

I want to call or execute javascript function when user click on image. I have javascript file with many functions and one of them is ShowKeybord(). So i want to execute this function ShowKeybord() when user clicks on image.

I tried with HTML parameter onclick: <img src = "' + content + '" alt = "Heads" onclick = "ShowKeyboard()" height="170" width="170"/> but its not working.

Here is my code of key functions that are related to my problem.

enter image description here

I tried with header.addEventListener('click', ShowKeyboard) and work properly but the keyboard is displayed when you click anywhere in the header of the web page. I would however like to make the keyboard appear only when a user clicks on the image.

What am I doing wrong and what should change depending on my code?

Thank you very much for your help.

Upvotes: 0

Views: 2893

Answers (3)

Ashraf.Shk786
Ashraf.Shk786

Reputation: 622

It's better to enclose your image inside a div tag. As suggested by @Lucas Duval. This should definitely work if not please give your div an "ID" or "class" and try to handle this with JQuery.

Thanks!

Upvotes: 1

Lucas Duval
Lucas Duval

Reputation: 43

You can also try to encapsulate your img with a div with the attribute onclick="ShowKeyboard()"

Wich gives something like this

<div onclick = "ShowKeyboard()">
<img src = "' + content + '" alt ="Heads" height="170" width="170"/>
</div>

Upvotes: 2

user7090116
user7090116

Reputation:

in the head tag put

<script src="yourjavascriptfilepath"></script>

and should work.

or maybe try onclick="ShowKeyboard()" rather than onclick = "ShowKeyboard()"

I think that has to do something with it.

Upvotes: 1

Related Questions