0

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";
      }
    }
  }
}
Progman
  • 16,827
  • 6
  • 33
  • 48
macuser
  • 1
  • 3

1 Answers1

0

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/

flakerimi
  • 2,580
  • 3
  • 29
  • 49