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'); } 

http://jsfiddle.net/y66wh26k/3/

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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -