I know that when using jQuery you can do $(\'element\').click();
to catch the click event of an HTML element, but how do you do that with plain Javascript?
I would recommend going with addEventListener
instead of assigning the handler function directly.
var div = document.getElementById('test');
div.addEventListener('click', function(){
console.log('CLICKED');
});
There are several reasons for that by I am going to name those I find the most important:
addEventListener
- your code would fail instead of quietly assigning onclick
function to some objectonclick
- something that might prove limiting