JulianJ JulianJ - 1 month ago 5
jQuery Question

Why can I not use the jQuery variable?

I am trying to understand how to use the variable

this_table
in the piece of code below that appends data to a table. The variable
this_table
looks like this #myTable1. Jquery does not see it as a variable in the append section of the code.
$('#myTable > tbody > tr').append('<td>info,</td>');
works fine.

The jQuery

$(document).on('submit', '.myForm', function(e) {

var tableid = $(this).closest("div").find("table").attr("id"); //find table id
var this_table = '#' + tableid; //Add ID selector
$('this_table > tbody > tr').append('<td>info,</td>'); //Append to table

e.preventDefault();
});

Answer

Your variable is interpreted as a string. You need to concatenate the variable with the string...

Change this line:

    $('this_table > tbody > tr').append('<td>info,</td>'); //Append to table

To this:

    $(this_table + ' > tbody > tr').append('<td>info,</td>'); //Append to table