Jeepers Creepers Jeepers Creepers - 3 months ago 7
jQuery Question

Append table column value with text when id's match

I have a table where i need to append some text after a data change. The first column in the table row holds the ID which I check is equal to the variable id and then append the word 'Updated!' to the id in the table cell. I have the following code but from what I have tested out it doesnt seem to get the equality right between the table row id and the variable id values. Can someone point me in the right direction for this to work please?

var id = $('#id').val();

$("td:first-child").each(function() {
if ($(this).text() == id){
$('<p>Updated!</p>').appendTo('$(this).text()');
}
});

Answer

You are not using appendTo (http://api.jquery.com/appendto/) properly. Please check the docs. When you append some element, you must append it to another element (not text). Also, you have quotes around the wrong piece $(this).text(), so that's a string and we're looking for an element. Try changing it to this:

var id =  $('#id').val();

$("td:first-child").each(function() {
    if ($(this).text() == id){
        $('<p>Updated!</p>').appendTo($(this));
    }
});