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?

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

Comments

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.