How can i make it so i don't need an onclick function in my html and the function is called in the js file [duplicate]
Trying to make a div respond to click events.
This is what I've tried:
<div onclick(console.log("Hello"))>1</div>
<div onclick(console.log(add(2, 5)))>1</div>
<div onClick(function(){ console.log "Hello" })>1</div>
Here is my code: http://jsfiddle.net/kastasten/vu3xo3am/5/
edit: Had forgotten to update the jsfiddle
Solution 1:
For an inline click event, this is what you need:
<div onclick='console.log("Hello")'>1</div>
http://jsfiddle.net/vu3xo3am/2/
Or,
If you want to do this unobtrusively, this would be how you could do it:
<div id="test">1</div>
var test = document.getElementById('test');
test.onclick = function() {
console.log('Hello');
}
http://jsfiddle.net/vu3xo3am/3/