You could bind the click
event in window.onload
only for elements with that attribute, like:
window.onload = function () {
var moves = document.querySelectorAll('[data-option="moveable"]');
for (var i = 0; i < moves.length; i++) {
moves[i].onclick = function (e) {
e = e || window.event;
moveableFunc(e, this);
};
}
};
You could even change the selector to be .querySelectorAll('div[data-option="moveable"]');
if you know this will only be targeting <div>
s, so it will speed up the search.
Or if you were able to restructure your moveableFunc
's parameters to be:
function moveableFunc(e) {
// Use `this` to reference the element that was clicked
}
Then you could just use the following to bind the event in the loop:
moves[i].onclick = moveableFunc;
Of course, it could be preferable to use addEventListener
instead of onclick
to bind the events. Here's some information on that: addEventListener vs onclick
References:
And just as an example, with jQuery, it could be done like:
$('[data-option="moveable"]').on("click", moveableFunc);
(where you modified the parameters of moveableFunc
like I said before)