Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to inline scripting in JavaScript

I am aware that in-line scripting should be avoided by keeping html/css and javascript separated.

How is it possible to do such a thing in the following code? //consider above in the code the function alertMe() declared

<img src="LittleBrain.png"  id="littlebrain" onClick="alertMe();">

How would it be possible not using in line code?

Thanks in advance.

like image 247
Rollerball Avatar asked Oct 24 '13 20:10

Rollerball


1 Answers

With addEventListener():

var img = document.getElementById('littlebrain');
img.addEventListener('click', function(event){
  alertMe();
});
like image 179
nice ass Avatar answered Sep 21 '22 10:09

nice ass