how to get a row index in dynamic table via jQuery

user3003466

I use jquery to dynamic create table content in page, how can I get the row number index of this dynamic tale? In the following code, when I click the "show more" link, I use index function in following code, but it now work. How to solve this issue? Thanks.

 $.getJSON(JsonURL,
        function(data){

            $.each(data, function(index, item){
                var obj = item;
                books.push(book);
            });
            for (var i = 0; i<obj.length; i++) {
                var tr=$('<tr></tr>'); 
                $('<td>'+ obj[i].name +'</td>').appendTo(tr);
                $('<td>'+ obj[i].category +'</td>').appendTo(tr);
                $('<td><a class=\'showMore\' href=#>'+ 'show more' +'</a></td>').appendTo(tr);
                tr.appendTo('#tableBody');
            };
    });
    $('a .showMore').on('click', function(event) {
        var rowindex = $(this).parent().parent().children().index($(this).parent);
        console.debug('rowindex', rowindex);
    });
Felix

You need to use event delegation for dynamically created elements:

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all children matching a selector, whether those children exist now or are added in the future.

$('body').on('click', 'a.showMore', function(event) {
    var rowindex = $(this).closest('tr').index();
    console.debug('rowindex', rowindex);
});

Please note that you dont need a space between a and .showmore because a .showmore will select any elements with class showmore which is a child of an anchor.

Also, .parent() is a method so you need () after parent if you want to use .parent().

Another suggestion is that instead of multiple parent() method, you can use .closest()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the row index in a HTML table

From Dev

Jquery get row value in a table

From Dev

Get table row by row index jquery

From Dev

get a row from dynamic table (jquery)

From Dev

How to add attribute to every table row only when one is hovered (and not the hovered row) via jQuery?

From Dev

jQuery, how to get value of a specific checkbox in table row?

From Dev

How to get table columns in display order via jQuery

From Dev

How to get dynamic created table's total column and row using Request.Form in vb.net

From Dev

jQuery delete table row by dynamic id

From Dev

Add row to table and send to server, post table via jquery to php

From Dev

How do I create table using row & col index in jQuery?

From Dev

Boostrap Table: get the row data by row index

From Dev

How to create a dynamic table in jQuery?

From Dev

How I remove table dynamic row?

From Dev

How to delete a specific row of a dynamic table using JQuery

From Dev

How to delete a specific row of a dynamic table after prompting the user using JQuery?

From Dev

How to get selected row index of a dynamic datatable flutter

From Dev

How to get the Row index and Columns index in the datagridView

From Dev

Jquery get row value in a table

From Dev

jQuery Datatables - How to get the index of a row when a button in that row is clicked

From Dev

get a row from dynamic table (jquery)

From Dev

JQuery dynamic binding on table row

From Dev

Jquery table row index after move

From Dev

Removing a row via jQuery, how to show "No Data" message if table is empty?

From Dev

How to get every id on a row in a table with jQuery?

From Dev

Get row id of dynamic table in html

From Dev

Get index of a table cell relative to the row

From Dev

How to delete table certain table row created from dynamic dataTables using jquery?

From Dev

Jquery get row Index

Related Related

  1. 1

    How to get the row index in a HTML table

  2. 2

    Jquery get row value in a table

  3. 3

    Get table row by row index jquery

  4. 4

    get a row from dynamic table (jquery)

  5. 5

    How to add attribute to every table row only when one is hovered (and not the hovered row) via jQuery?

  6. 6

    jQuery, how to get value of a specific checkbox in table row?

  7. 7

    How to get table columns in display order via jQuery

  8. 8

    How to get dynamic created table's total column and row using Request.Form in vb.net

  9. 9

    jQuery delete table row by dynamic id

  10. 10

    Add row to table and send to server, post table via jquery to php

  11. 11

    How do I create table using row & col index in jQuery?

  12. 12

    Boostrap Table: get the row data by row index

  13. 13

    How to create a dynamic table in jQuery?

  14. 14

    How I remove table dynamic row?

  15. 15

    How to delete a specific row of a dynamic table using JQuery

  16. 16

    How to delete a specific row of a dynamic table after prompting the user using JQuery?

  17. 17

    How to get selected row index of a dynamic datatable flutter

  18. 18

    How to get the Row index and Columns index in the datagridView

  19. 19

    Jquery get row value in a table

  20. 20

    jQuery Datatables - How to get the index of a row when a button in that row is clicked

  21. 21

    get a row from dynamic table (jquery)

  22. 22

    JQuery dynamic binding on table row

  23. 23

    Jquery table row index after move

  24. 24

    Removing a row via jQuery, how to show "No Data" message if table is empty?

  25. 25

    How to get every id on a row in a table with jQuery?

  26. 26

    Get row id of dynamic table in html

  27. 27

    Get index of a table cell relative to the row

  28. 28

    How to delete table certain table row created from dynamic dataTables using jquery?

  29. 29

    Jquery get row Index

HotTag

Archive