1

I want to use a jQuery argument in a JavaScript function. At the moment, my code looks like this:

window.nextGen = function(cell) {
    // code...
} 

window.prepareNextGen = function() {
    nextGen($('#grundtabelle').rows[1].cells[1]);
}

But that doesn't work. Any help?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
kangalio
  • 652
  • 8
  • 16

2 Answers2

1

Simple Fix

To access the table object rows and cells you can simply add an array index like so:

nextGen( $('#grundtabelle')[0].rows[1].cells[1] );

See this previous question for more detail: How to get a DOM Element from a JQuery Selector

Run the snippet to try

nextGen( $('#grundtabelle')[0].rows[1].cells[1] );



function nextGen( cell ) {
  
  console.info( cell.innerHTML );  // displays B1
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<table id="grundtabelle">
  <tr>
    <td>A0</td>
    <td>B0</td>
  </tr>
  <tr>
    <td>A1</td>
    <td>B1</td>
  </tr>
  <tr>
    <td>A2</td>
    <td>B2</td>
  </tr>
</table>
Community
  • 1
  • 1
Yogi
  • 6,241
  • 3
  • 24
  • 30
0

A jQuery object does not have a rows or cells property. Assuming you're trying to get the second td of the second tr (note that the indexes are zero-based, so the item with index of 1 is the second), then you need to use jQuery's DOM traversal methods. In this case, find() and :eq. Try this:

nextGen($('#grundtabelle').find('tr:eq(1) td:eq(1)'));

If the nextGen() function is expecting a DOMElement instead of a jQuery object then you can retrieve that from the jQuery object like this:

nextGen($('#grundtabelle').find('tr:eq(1) td:eq(1)')[0]);
// or:
nextGen($('#grundtabelle').find('tr:eq(1) td:eq(1)').get());
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339