6

How to remove only text from td?

This is my HTML td:

<td class="SmallCols PadOn">
    6 
    <input type="hidden" id="HiddenID" value="0" name="HiddenID">
</td>

6- the only text inside td. There is tdalso a hidden field that I do not want to delete. I just want to delete the text 6. I tried this code but no luck:

var cloneTr = $('#StudentGrid tr:last').clone();
cloneTr.closest('td').contents().filter(function () {
    return this.nodeType === 3;
}).remove().end().end();

We are looking for help and suggestions. Thanks

+4
source share
1 answer

The problem is that it closest()goes up the DOM tree, while you need to go down the tree to find the child, so you should use it instead find(). Also, calls end()are redundant. Try the following:

var cloneTr = $('#StudentGrid tr:last').clone();
cloneTr.find('td').contents().filter(function() {
    return this.nodeType === 3;
}).remove();

Script example

+1
source

Source: https://habr.com/ru/post/1625114/


All Articles