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.

Answers:

Answer

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.

Answer
var $current = $('#current');
$current.replaceWith($current.text());

Fiddle with the two current answers - pun?

unwrap vs replaceWith

Answer

Just hit this same question. I think this might be marginally better...same effect but I think jQuery has to do less magic to make this work.

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.