My own javascript class and addeventlistener template not working -
so here experimenting native javascript. trying achieve, failed create sort of shortcut scenarios want add event listener div class.
here trying say: if user clicked on class show alert.
why doesnt code work?
function click(red){ var source = document.getelementsbyclassname(red); (i = 0; < source.length; i++) { source[i].addeventlistener('click', err, false); } function err() { var x = 0; } } if (click('red')) { alert('rrr'); }
i use document.queryselectorall
instead of document.getelementsbyclassname
because more versatile.
but main issue click function wasn't returning anything, if(click('red'))
never execute. well, callback event listener being added wasn't doing anything. i've done made callback argument, passed addeventlistener
.
function click(selector, callback){ var source = document.queryselectorall(selector); (var = source.length-1; >= 0; --i) { source[i].addeventlistener('click', callback, false); } } click('.red', function() { alert('rrr'); });
<div class='red'>xxx</div> <div class='red'>xxx</div> <div class='red'>xxx</div>
Comments
Post a Comment