How to use onmousedown with grid in a for loop javascript

63 Views Asked by At

I am trying to get a right-click event to be used with grid in a for loop... I'm not sure if I can use onmousedown in a for loop? please check my code...

document.addEventListener("contextmenu", function (e) {
        e.preventDefault();
    }, false);

for (var i = Math.max(cellRow - 1, 0); i <= Math.min(cellRow + 1, 9); i++) {
  for (var j = Math.max(cellCol - 1, 0); j <= Math.min(cellCol + 1, 9); j++) {
    grid.rows[i].cells[j].onmousedown = function (event) {
      if (event.which == 3) {
        grid.rows[i].cells[j].innerHTML = "f";
      }
    }
  }
}
1

There are 1 best solutions below

0
Flak On

Play with e.target to get what you need.

document.addEventListener("contextmenu", function (e) {
        e.preventDefault();
        console.log(e.target.textContent);
    }, false);

You dont need this part:

grid.rows[i].cells[j].onmousedown = function (event) {
      if (event.which == 3) {
        grid.rows[i].cells[j].innerHTML = "f";
      }
    }

http://jsfiddle.net/RFFy9/