Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery select after get() insertion

I'm inserting rows into a table via a get() call to a php page, which works. I then want to update the row numbers of the table.

Jquery updates all the rows except the most recently inserted one.

$("#insert_row").click(function () {
    $.get("print_row.php", function(data){
        $("#table").append(data);
    });
});

$("#insert_row").on("click", function () {
    var rowNumber = 1;
        $(".rowNumColumn").each(function () {
            $(this).text(rowNumber);
            rowNumber++;
        })    
});

I've browsed the site for a while looking for an answer to this, and the solution appears to be the live() or on() modifiers. However I can't get that to work for me.

like image 462
Josh M. Avatar asked Mar 17 '26 14:03

Josh M.


1 Answers

You need to do the renumbering in the callback of the get. As it is now, you are renumbering prior to the get request returning. get is simply a shortcut for making an ajax get request. AJAX is asynchronous so it does not wait for the request to return before moving on.

$("#insert_row").click(function () {
    $.get("print_row.php", function(data){
        $("#table").append(data);

        var rowNumber = 1;
        $(".rowNumColumn").each(function () {
            $(this).text(rowNumber);
            rowNumber++;
        }) 
    });
});
like image 87
James Montagne Avatar answered Mar 19 '26 06:03

James Montagne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!