darkness
darkness

Reputation: 225

set variable function not working

function eventlisternOnclick(_class, ability, _src, _id){
  var cell_onclick = document.querySelectorAll(_class);
  for(var c = 0; c < cell_onclick.length; c++){
  cell_onclick[c].addEventListener('click', function(){
  //problem here , ability(_src) = fun1(mysrc);
  //error
    ability(_src);
  }, false);
  }
}

function fun1(mysrc){
console.log();
}

eventlisternOnclick("abc", "fun1", "this.src", null);

the fun1 is a function and i was trying to set as variable at eventlisterOnclick , and run the function there , but when i type ability(_src); it wont work. how do i set the function in my eventlisternOnclick.

Upvotes: 1

Views: 45

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

if the parameter passed is always class then replace

document.querySelectorAll(_class);

with

document.querySelectorAll("." + _class);

Also, fun1 is a string, so if fun1 is available to this scope then try

eventlisternOnclick("abc", fun1, "this.src", null);

Complete function could be

function eventlisternOnclick(_class, ability, _id){
  var cell_onclick = document.querySelectorAll(_class);
  for(var c = 0; c < cell_onclick.length; c++){
    cell_onclick[c].addEventListener('click', function(){
      ability(this.getAttribute("src"));
  }, false);
  }
}

function fun1(mysrc){
   console.log();
}

eventlisternOnclick("abc", fun1, null);

Upvotes: 1

Related Questions