Getting row id, not by clicking but from Keyup event

Getting row id, not by clicking but from Keyup event

TronikTronik Posts: 122Questions: 28Answers: 1

Hi!

Trying to get the row id, when user clicks enter in an input field on that row.
Changing the selectors do not help... Any suggestions on how to achieve this?

Current code just alerts undefined

$('#datatable tbody input').on( 'keyup change', function (ev) {

                    if (ev.keyCode == 13) {
                
                       alert( 'Row index: '+table.row( this ).id() );
                       return false;
                                     
                                   }
                } );

Thankful

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    There really isn't enough code here to go on. I think you have two issues with table.row( this ).id()

    1. this is the input and not a row selector. You need to get the row that the input is on. Something like this maybe:
    var row = $(this).closest('tr');
    `table.row( row ).id()
    
    1. For row().id() to work you need to have a row ID defined. As mentioned in the docs you may need to use rowId to define your row ID. Just a guess since you haven't provided this info.

    Kevin

  • TronikTronik Posts: 122Questions: 28Answers: 1

    Thank you!

    Obvious really... the examples in docs are targeted to TR element.

    As you pointed out id dont work but index did:

    table.row( $(this).closest('tr') ).index();

    And that gives me ID of row which is what I need

This discussion has been closed.