DolDurma
DolDurma

Reputation: 17321

jQuery get nth:child from table after creating that with ajax

in this below ajax i fill and create simple data after getting data

$.ajax({
    method: 'GET',
    url: '/analyzePage/searchTag/' + tagName,
    contentType: false,
    processData: false,
    success: function (data) {
        console.log(data);

        setTimeout(function () {
            if (data.state == 'error') {
                ...
            }
            else {
                let table = '<div class="table-responsive pre-scrollable">';

                ...

                $.each(data.tags, function (key, value) {
                    table = table + '<tr class="searchTag-' + value.name + '">';
                    table = table + '<td style="padding: 6px 20px;">1</td>';
                    table = table + '<td style="padding: 6px 0px;">';
                    table = table + '<img src="' + value.profile_pic_url + '" class="img-circle img-sm" id="tagIamge"/></td>';
                    table = table + '<td style="padding: 6px 20px;">' + value.name + '</td>';
                    table = table + '<td style="padding: 6px 20px;">' + value.media_count + '</td>';
                    table = table + '<td>';
                    table = table + '<button type="button" class="btn btn-warning btn-icon btn-rounded legitRipple" id="searchTag-' + value.name + '">';
                    table = table + '<i class="icon-file-plus"></i>';
                    table = table + '</button>';
                    table = table + '</td>';
                    table = table + '</tr>';
                });

                ...

                $('.data').html(table);
            }
        }, 800);
    },
    error: function (data) {
        console.log(data);
    }
});

in that i have one button id="searchTag-' + value.name + '" which its into latest td i want to get all columns that this button is into that, for example this tr have 4 td and into latest td i have this button, then i want to get other td into this tr after click on button

my code work but its get all trs into my table

$(document).on('click', "[id^='searchTag']", function () {
    let thisId = $(this).attr("id");
    let tagName = thisId.split("-")[1];
    $("[class^='" + $(this).attr("id") + "'] td:nth-child(3)").append("<span>Clicked</span>");
});

Upvotes: 1

Views: 75

Answers (1)

Undefined
Undefined

Reputation: 49

Do you want to add a span next to the icon of the button after clicking to it?

If you do, you can do it like this:

$(document).on('click', "[id^='searchTag']", function () {
    $(this).closest('td').append("<span>Clicked</span>");
});

i want to get all td which button is into this row

Try this:

$(document).on('click', "[id^='searchTag']", function () {
    $(this).closest('tr').find('td'); // returns all "td" elements
});

Upvotes: 1

Related Questions