Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwrap selected element Javascript or Jquery

This is my html.

<span id="current" class="green unselctable" data-original-title="" title="">
    Lorem Ipsum is simply dummy
</span>

I'm selecting with with $("#current") but if I use the jQuery unwrap function the parent tag gets removed.

Is there any way to remove the span in JavaScript or jQuery without parsing the string and appending it to the dom?

Edit I wanna keep the content of the div. I just want the tag removed.

like image 435
Bobby Tables Avatar asked Apr 02 '13 19:04

Bobby Tables


People also ask

How to unwrap an element in jQuery?

The unwrap() method is an inbuilt method in jQuery which is used to remove the parent element from the selected element. Parameters: This method does not accept any parameter. Return Value: This method returns the selected element with the changes made by unwrap() method.

What is unwrap () in JS?

unwrap() method removes the element's parent and returns the unwrapped content. This is effectively the inverse of the . wrap() method. The matched elements (and their siblings, if any) replace their parents within the DOM structure.

What is wrap and unwrap in jQuery?

createElement() to create an element, and wrap it around each selected element. Wrap elements using a function. Using a function to specify what to wrap around each selected element. Wrap and unwrap an element. How to toggle between wrapping and unwrapping an element.

What is the use of unwrap method?

The unwrap() method removes the parent element of the selected elements.


1 Answers

You can chain contents() into unwrap():

$("#current").contents().unwrap();

contents() will return all the children of #current, including text nodes, and unwrap() can be applied to text nodes.

like image 56
Frédéric Hamidi Avatar answered Sep 28 '22 16:09

Frédéric Hamidi