Select Cells On A Table By Dragging

I was looking at this question and saw the reference to the iPhone game where you drag across the screen selecting letters as you go.

I was curious to see an implimentation of this in Javascript using tables. So you'd drag the mouse over each cell they would then become highlighted.

I'm not sure what the best method would be but I hope someone has a go. Someone attempted it here, but it doesn't really work.

alt text alt text

Thank Cacoo for the sexy diagrams. It's like an online visio, really nice. Check it out ;)



Here's a working prototype: Using jQuery for DOM hooking, but could easily be implemented with another framework.

Update: a slightly different version - the highlighted state will only change when you release and click again.

Update 2: - binding onselectstart so that text is not selected in IE.

A few relevant facts:

  • The mousedown event of the table cells is hooked to track the actual click. This event is stopped, so that text selection is hindered. Also binding ontextselect for the same effect in IE.
  • The mouseover event will toggle the highlighted class for the cell
  • The mouseout event is hooked on document. This is to ensure that it always runs. If the mouseup event was hooked on the table cell, it would not trigger if you released the mouse key with the mouse outside of the table. This state is tracked in isMouseDown.

Full source code for reference:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" xml:lang="en" lang="en">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <style type="text/css" media="screen">
    table td {

    table td.highlighted {
  <table cellpadding="0" cellspacing="1" id="our_table">

  <script src="" type="text/javascript"></script>
  <script type="text/javascript" charset="utf-8">
    $(function () {
      var isMouseDown = false;
      $("#our_table td")
        .mousedown(function () {
          isMouseDown = true;
          return false; // prevent text selection
        .mouseover(function () {
          if (isMouseDown) {
        .bind("selectstart", function () {
          return false; // prevent text selection in IE

        .mouseup(function () {
          isMouseDown = false;

If you're after spreadsheet-like cell selection (in column/row blocks), you need to highlight every cell in every row that is between your start & end index (both row and cell) in your mouseover event:

for (var i = rowStart; i <= rowEnd; i++) {
    var rowCells = table.find("tr").eq(i).find("td");
    for (var j = cellStart; j <= cellEnd; j++) {

As the user might start selecting cells from all directions (up-bottom, bottom-up, right-left, left-right) you need to assign the correct indexes for start & end.

Here is a jsFiddle.


Not sure if you wanted pure-Javascript implementation, I used jQuery for convenience.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.