Alternative of onclick for html5

user3731917 picture user3731917 · Jun 12, 2014 · Viewed 10.8k times · Source
 <div id="btnSideToggle" onclick="Viewer.toggleThumbnails();"><img src="assets/pgThumbs.png"></div>

Due to CSP restriction on Firefox OS Marketplace i cant use onclick on html5 page, can anyone please tell me alternative of onclick for HTML5

here is the full code : http://jsfiddle.net/edd4225/v65qZ/

Answer

maček picture maček · Jun 12, 2014

This will work for you

var button = document.querySelector("#btnSideToggle");

button.addEventListener("click", function onclick(event) {
  Viewer.toggleThumbnails();
  event.preventDefault();
});

You could simplify it with Function.prototype.bind (requires ECMAScript >= 5)

button.addEventListener("click", Viewer.toggleThumbnails.bind(Viewer));

Note that an Event will still be sent as the first argument to the toggleThumbnails handler.