Jeepers Creepers
Jeepers Creepers

Reputation: 115

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()');
            }
        });

Upvotes: 0

Views: 478

Answers (3)

Jeepers Creepers
Jeepers Creepers

Reputation: 115

I found what the issue was with the equality between the 2 variables. I needed to trim() the values.

Upvotes: 0

kosmos
kosmos

Reputation: 4288

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));
    }
});

Upvotes: 3

Damian Bartosik
Damian Bartosik

Reputation: 498

Read more about appendTo() function. It should look like that:

$('<p>Updated!</p>').appendTo($(this));

It basically appends text or html content to and element, not to a string (which you provided).


appendTo() on jQuery API Dosc: http://api.jquery.com/appendto/

Upvotes: 0

Related Questions