Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove text with jQuery

Tags:

jquery

Is there a way to remove text that is not wrapped in any tag using jQuery

<p>This is some text</p>  This is "unwrapped" text //to be removed  <span>some more text</span> 

Thank you for your help

like image 551
Dom Avatar asked Oct 15 '09 08:10

Dom


People also ask

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

What does addClass do in jQuery?

jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

Is empty in jQuery?

The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.


1 Answers

Using the answer from this question:

$(elem)   .contents()   .filter(function() {     return this.nodeType == 3; //Node.TEXT_NODE   }).remove(); 
like image 89
Greg Avatar answered Sep 28 '22 02:09

Greg