Ruth Young
Ruth Young

Reputation: 888

How can I append some html inside <td> using jquery

I'm trying to add a <hr> element into a <td> in jquery based on the class. I've managed to find the <td> elements I want to append without a problem, but I can't add the html - the <td> already contains some html, I want to append this on the end.

Here is what I'm trying:

<script>
$( document ).ready(function() {
    var something = "something";
    var element = $('td').filter(':contains(something)').html('<p>Hello</p>');
            console.log(element);
});
</script>

Thanks

Upvotes: 0

Views: 6908

Answers (3)

Shender Ramos
Shender Ramos

Reputation: 53

try appending..something like this... and make sure you're using the jquery library

var element =$('td').filter(':contains(something)').append('<p>Hello</p>');

Upvotes: 0

Shtut
Shtut

Reputation: 1407

Instead of using .html() you can use .append() which just added whatever you write into the end of the div.

Like this

$('td').filter(':contains(something)').append('<p>Hello</p>')

This will put hello after filtered <td>

You can also use .prepend() to add things to the begining of the div.

Upvotes: 4

Roy Bogado
Roy Bogado

Reputation: 4472

Take a look to this: https://jsfiddle.net/jo2t5yL0/1/
It's working with <td> and .html() Maybe you forget to include jquery lib?

Upvotes: 0

Related Questions