remove - jquery set text without child elements




jQuery: how to remove the text but not the children elements (4)

can I remove with jQuery only the text in a node but not the children elements ?

thanks


Best and easy solution to remove all text elements from certain div is

$("#firstDiv").contents().filter(function(){
    return (this.nodeType == 3);
}).remove();

Here's my idea:

function removeText(element){
    var newElement = $('<' + element[0].nodeName + '/>');
    element.children().each(function(){
        newElement.append(this);
    });
    element.replaceWith(newElement);
}

you cannot do that.Unless it is wrapped by an element.


$('#someElement').text('');






jquery