Borneyak
Borneyak

Reputation: 47

Javascript/jquery: Remove parent of class and keep it's child

I'd like to remove a parent of a child with specific classname with javascript/jquery. It's a span wrapper which i'd like to remove. I only know the class (or id) of the child to work with.

How can i do that?

So this:

<span>
  <input class="removespan" type="text">
</span>

Needs to be this:

<input class="removespan" type="text">

Upvotes: 1

Views: 286

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075457

Amazingly, jQuery has a method for doing exactly that: unwrap

$(".removespan").unwrap();

Live example:

setTimeout(function() {
  $(".removespan").unwrap();
}, 1000);
span {
  border: 1px solid green;
  padding: 4px;
}
<div style="margin-bottom: 8px">
The span has a green border. The span is removed after a second:
</div>
<div>
<span>
  <input class="removespan" type="text">
</span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 3

Related Questions