Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Check" Multiple Checkboxes With Click & Drag?

Tags:

html

checkbox

I have a table filled with checkboxes like so:

enter image description here

I'd like to be able to keep my mouse held down and drag to activate multiple checkboxes. I don't have the slightest clue where to start with this :/ I searched for an answer, but only found another thread of someone asking how to do it, but with no answers.

HTML:

<table>
  <tbody>
    <tr>
      <td><input type="checkbox"></td>
      <td><input type="checkbox"></td>
      <td><input type="checkbox"></td>
    </tr>
    <!-- Repeat tr 2x -->
  </tbody>
</table>

jsFiddle: https://jsfiddle.net/CSS_Apprentice/ge1zx2yg/

Also, I'd prefer to keep the <input type="checkbox"> model because reworking my system would be time consuming, but I'm open to other options if this isn't possible. Any help will be greatly appreciated!

like image 208
CSS Apprentice Avatar asked Apr 20 '16 20:04

CSS Apprentice


1 Answers

<table>
  <tbody>
    <tr>
      <td><input id=1 onmouseover='check(1)' type="checkbox"></td>
      <td><input id=2 onmouseover='check(2)' type="checkbox"></td>
      <td><input id=3 onmouseover='check(3)' type="checkbox"></td>
    </tr>
    <tr>
      <td><input id=4 onmouseover='check(4)' type="checkbox"></td>
      <td><input id=5 onmouseover='check(5)' type="checkbox"></td>
      <td><input id=6 onmouseover='check(6)' type="checkbox"></td>
    </tr>
    <tr>
      <td><input id=7 onmouseover='check(7)' type="checkbox"></td>
      <td><input id=8 onmouseover='check(8)' type="checkbox"></td>
      <td><input id=9 onmouseover='check(9)' type="checkbox"></td>
    </tr>
  </tbody>
</table>

<script>
  function check(id)
  {
    if(mouseDown)
      {
        document.getElementById(id).checked = 1-document.getElementById(id).checked;
        // document.getElementById(id).checked = true;
        // ^ If you only want to turn them on, use this.
      }
  }
  
  var mouseDown = 0;
  document.body.onmousedown = function()
  { 
    ++mouseDown;
  }
  
  document.body.onmouseup = function()
  {
  --mouseDown;
  }
  // Credit to http://stackoverflow.com/questions/322378/javascript-check-if-mouse-button-down
</script>

Or, alternatively, use the code beneath to avoid IDs:

<table>
  <tbody>
    <tr>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
    </tr>
    <tr>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
    </tr>
	<tr>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
      <td><input onmouseover='check(this)' type="checkbox"></td>
    </tr>
  </tbody>
</table>

<script>
  function check(box)
  {
    if(mouseDown)
      {
        box.checked = 1-box.checked;
		// box.checked = 1;
        // ^ If you only want to turn them on, use this.
      }
  }
  
  var mouseDown = 0;
  document.body.onmousedown = function()
  {++mouseDown;}
  document.body.onmouseup = function()
  {--mouseDown;}
  // Credit to http://stackoverflow.com/questions/322378/javascript-check-if-mouse-button-down
</script>
like image 182
StardustGogeta Avatar answered Sep 23 '22 20:09

StardustGogeta