There is a table, when you click on any first cell, the initExpandableTableRows() method is initExpandableTableRows() , and if you click on any other cell, the initTableRows() method is initTableRows() . The code is incorrect. I'm a newbie

 function addOnclickToDatatableRows() { var tds = $('[id*=dataTable] tr td:first-child').get(); var trs = $('[id*=dataTable] tbody:first tr').get(); for (var i = 0; i < tds.length; i++) { tds[i].onclick = new Function("selectFirstCell()"); trs[i].onclick = new Function("selectRow()"); } } function selectFirstCell() { initExpandableTableRows(); } function selectRow(){ initTableRows(); } 
  • But where does this code come from? - Grundy

2 answers 2

Your example is similar to pseudocode, but from it, if I understood everything correctly, the answer might be:

 $rows = $('table tr'); $cols_first = $rows.find('td:first-of-type'); $cols_secnd = $rows.find('td:not(:first-of-type)'); $cols_first.click(function() { alert('cols_first'); }); $cols_secnd.click(function() { alert('cols_secnd'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> </table> 

Clarify the question if the code does not what you asked.

  • why pseudocode? quite working - Grundy
  • Then we wait for the author, the question is not clear to me until the end ... - doubleui

You can even check for the index - .index() :

 var table = $('table'), trs = table.find('tr'), tds = trs.find('td'); tds.click(function(){ if($(this).index() === 0){ selectFirstCell(); }else if($(this).index() !== 0){ selectRow(); } }); function selectFirstCell() { // initExpandableTableRows(); alert('initExpandableTableRows'); } function selectRow(){ // initTableRows(); alert('initTableRows'); } 
 td { border: 1px solid #ccc; padding: 1rem 2rem; cursor: pointer; } 
 <script src="https://code.jquery.com/jquery-2.0.3.js"></script> <table> <tr> <td>1</td><td>1</td><td>1</td> </tr> <tr> <td>2</td><td>2</td><td>2</td> </tr> <tr> <td>3</td><td>3</td><td>3</td> </tr> </table>