Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click anywhere on a table row and it will check the checkbox its in...?

I'm trying to figure out how to do this, but I can't find it out. Basically I want to be able to click anywhere on a table row and it will check/ unchecked the checkbox its in. I know it's possible because that's what PHPMyAdmin does...

here is my table row

<tbody>
<tr id="1" onclick="selectRow(1)"><td width="20px"><input type="checkbox" id="1" name="1/"></td><td>1</td><td>2011-04-21 22:04:56</td><td>action</td></tr>

<tr id="2" onclick="selectRow(2)"><td width="20px"><input type="checkbox" id="2" name="2/"></td><td>2</td><td>2011-04-21 22:04:56</td><td>action</td></tr>

</tbody>
like image 266
saint Avatar asked Apr 29 '11 14:04

saint


3 Answers

Try this one out...

$("tr").click(function() {
    var checkbox = $(this).find("input[type='checkbox']");
    checkbox.attr('checked', !checkbox.attr('checked'));
});

http://jsfiddle.net/dVay8/

like image 127
Duda Nogueira Avatar answered Nov 16 '22 08:11

Duda Nogueira


<script type="text/javascript">
function selectRow(row)
{
    var firstInput = row.getElementsByTagName('input')[0];
    firstInput.checked = !firstInput.checked;
}
</script>

...

<tbody>
    <tr onclick="selectRow(this)"><td width="20px"><input type="checkbox" id="chk1" name="chk1/"></td><td>1</td><td>2011-04-21 22:04:56</td><td>action</td></tr>    
    <tr onclick="selectRow(this)"><td width="20px"><input type="checkbox" id="chk2" name="chk2/"></td><td>2</td><td>2011-04-21 22:04:56</td><td>action</td></tr>  
</tbody>

Note: You've also got collisions on ids. Your ids should be unique.

Here's an alternative with programmatic binding:

document.querySelector("table").addEventListener("click", ({target}) => {
  // discard direct clicks on input elements
  if (target.nodeName === "INPUT") return;
  // get the nearest tr
  const tr = target.closest("tr");
  if (tr) {
    // if it exists, get the first checkbox
    const checkbox = tr.querySelector("input[type='checkbox']");
    if (checkbox) {
      // if it exists, toggle the checked property
      checkbox.checked = !checkbox.checked;
    }
  }
});
<table>
  <tbody>
    <tr>
      <td>
        <input type="checkbox" id="chk1" name="chk1" />
      </td>
      <td>1</td>
      <td>2011-04-21 22:04:56</td>
      <td>action</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" id="chk2" name="chk2" />
      </td>
      <td>2</td>
      <td>2011-04-21 22:04:56</td>
      <td>action</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" id="chk2" name="chk3" />
      </td>
      <td>2</td>
      <td>2011-04-21 25:30:16</td>
      <td>action</td>
    </tr>
  </tbody>
</table>
like image 41
canon Avatar answered Nov 16 '22 09:11

canon


You don't need JavaScript for this:

td label {
  display: block;
}
<td width="20px"><input type="checkbox" id="chk2" name="chk2/"></td><td><label for="chk2">2</label></td><td><label for="chk2">2011-04-21 22:04:56</label></td><td><label for="chk2">action</label></td>

Just labels and a little CSS.

like image 7
Gerald Schneider Avatar answered Nov 16 '22 08:11

Gerald Schneider