javascript - How can I append some html inside <td> using jquery - Stack Overflow

I'm trying to add a <hr> element into a <td> in jquery based on the class. I've m

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

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

Share Improve this question edited Feb 21, 2017 at 12:25 Mayank Patel 1,5711 gold badge14 silver badges20 bronze badges asked Feb 21, 2017 at 12:15 Ruth YoungRuth Young 8882 gold badges15 silver badges38 bronze badges 4
  • 1 Can you please share markup as well? – Rajesh Commented Feb 21, 2017 at 12:16
  • 6 Then use .append() instead of .html() – Andreas Commented Feb 21, 2017 at 12:17
  • 1 filter(':contains(something)') would be filter(':contains('+something+')') – Roy Bogado Commented Feb 21, 2017 at 12:18
  • My filter wasn't working properly @Roy this was the fix thanks – Ruth Young Commented Feb 21, 2017 at 12:24
Add a ment  | 

3 Answers 3

Reset to default 4

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.

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745331432a4622895.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信