-2

The function below toggles the highlight on a cell when a user clicks the cell.

It works well if the cells are there when the document loads. However, it does not work on newly appended cells.

What could I change to make it work for new cells?

  $(function () {
  var isMouseDown = false,
    isHighlighted;

  $("#tablegrid td.nohighlight")
    .mousedown(function () {
      isMouseDown = true;
      $(this).toggleClass("highlighted");
      isHighlighted = $(this).hasClass("highlighted");
      return false; // prevent text selection
    })
    .mouseover(function () {
      if (isMouseDown) {
        $(this).toggleClass("highlighted", isHighlighted);
      }
    })
    .bind("selectstart", function () {
      return false;
    })

  $(document)
    .mouseup(function () {
      isMouseDown = false;
    });
});
user749798
  • 5,210
  • 10
  • 51
  • 85

1 Answers1

0
$("#tablegrid").on('mousedown', 'td.nohighlight', function() {
....
}

Same stuff for 'mouseover' and 'bind'.

Blallo
  • 450
  • 3
  • 11