Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking a row except for the first td

I am trying to click a table row and perform an action. However if they click the first <td> cell I do not want that to perform the action.

Here is the code I have so far:

jQuery('#dyntable tbody tr').live('click', function () {
        nTr = jQuery(this)[0];

        openMessage(oTable, nTr);
    });

This works as intended, except the first <td> has a check box, so if they click in that cell I do not want to call the openMessage(oTable, nTr); function.

I also still need nTr to = the contents of the row.

like image 279
James Wilson Avatar asked Jun 20 '12 17:06

James Wilson


3 Answers

Use target of click within row, and check the index of TD

Simplified DEMO: http://jsfiddle.net/WLR9E/

jQuery('#dyntable tbody tr').live('click', function (evt) {
    var $cell=$(evt.target).closest('td');
    if( $cell.index()>0){
       openMessage(oTable, this);
}
});

live() is deprecated , if using jQuery >= 1.7 convert to on(). Following assumes main table is a permanent asset in page.

jQuery('#dyntable').on('click','tbody tr', function (evt) {
    var $cell=$(evt.target).closest('td');
    if( $cell.index()>0){
       openMessage(oTable, this);
   }
});

This line in your code is redindant, it simply returns the same as this

nTr = jQuery(this)[0];
like image 110
charlietfl Avatar answered Nov 11 '22 16:11

charlietfl


 $("#dyntable tbody").on('click', 'tr td:not(:first-child)',function () {
    alert("success");
});

$(document).ready(function ()
{
   $("#tbluserdata tbody").on('click', 'tr td:not(:first-child)', function () {
      alert("write code");
    });
  
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
    </head>
  <body>
    <table border="2" width="50%" id="tbluserdata">
      <thead>
        <tr>
          <th>Id</th>
        <th>Name</th>
        <th>Address</th>
          <th>Action</th>
          </tr>
        </thead>
      <tbody>
      <tr>
        <td>1</td>
        <td>aaa</td>
        <td>ccc</td>
         <td>delete</td>
        </tr>
      <tr>
        <td>1</td>
        <td>aaa</td>
        <td>ccc</td>
        <td>delete</td>
        </tr>
      <tr>
        <td>1</td>
        <td>aaa</td>
        <td>ccc</td>
        <td>delete</td>
        </tr>
        </tbody>
      </table>
    </body>
</html>
like image 10
Lalji Dhameliya Avatar answered Nov 11 '22 17:11

Lalji Dhameliya


This'll do the trick:

jQuery('#dyntable tbody tr td:not(:first)').live('click', function () {
        nTr = jQuery(this).parent('tr')[0];

        openMessage("asdf", nTr);
});


function openMessage(oTable, nTr){
  console.log(nTr);
  alert('something happened');    
}

Here's the fiddle to test: DEMO

like image 2
jeschafe Avatar answered Nov 11 '22 17:11

jeschafe