locked
how can i get row index after focusing on it RRS feed

  • Question

  • User-1634604574 posted

    i have this table i want when i focused on each row get row index

      $("#Items_tbl").off("focus", "tr").on("focus", "tbody tr", function () {
    
                   var i = $(this).parent().index(); 
    
    
    })

    but this code is not correct when i add a new row by button click every time show row index =1

    i have input inside each td

    Friday, October 25, 2019 10:25 PM

Answers

  • User665608656 posted

    Hi zhyanadil,

    As @bruce said, this event is triggered when you foucs tr, so $(this) here represents your current click row.

    If you want to get the index of the current line, you don't need to get it through parent(). You can write as follows:

     var i = $(this).index();

    If you want to get the value of a specific td, you can write like this:

     var id = $(this).find("td").eq(0).html(); // The 0 here represents the index of td under the current tr.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 28, 2019 3:10 AM

All replies

  • User-474980206 posted

    $(this) is a tr and it’s parent() is a tbody, and tbody is always index 1 of its parent because there is only one.

    Saturday, October 26, 2019 3:10 PM
  • User665608656 posted

    Hi zhyanadil,

    As @bruce said, this event is triggered when you foucs tr, so $(this) here represents your current click row.

    If you want to get the index of the current line, you don't need to get it through parent(). You can write as follows:

     var i = $(this).index();

    If you want to get the value of a specific td, you can write like this:

     var id = $(this).find("td").eq(0).html(); // The 0 here represents the index of td under the current tr.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 28, 2019 3:10 AM