can I remove with jQuery only the text in a node but not the children elements ?
thanks
The jQuery empty() method removes the child elements of the selected element(s).
The best way is to . clone() your object, . remove() all of its . children() , then go back to the object using .
version added: 1.4. The . detach() method is the same as . remove() , except that . detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.
To remove all attributes of elements, we use removeAttributeNode() method.
Best and easy solution to remove all text elements from certain div is
$("#firstDiv").contents().filter(function(){ return (this.nodeType == 3); }).remove();
This works for me:
$(elem).html($(elem).html().replace($(elem).text(),''));
This set the html content without the text content, it means that any html tag inside of elem will be keep without changes.
before:
<div>Hello<input type="text" name="username" value="world!"/></div>
after:
<div><input type="text" name="username" value="world!"/></div>
bye
Here's my idea:
function removeText(element){
var newElement = $('<' + element[0].nodeName + '/>');
for(i=0; i < item.attributes.length; i++) {
newElement.attr(item.attributes[i].name, item.attributes[i].value);
}
element.children().each(function(){
newElement.append(this);
});
element.replaceWith(newElement);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With