Have you tried using the unwrap()
method in the jQuery library? If it leaves text and comments in place, you can reduce your code to:
$('#my_span').unwrap();
If you do not want all children to be deleted, you can extend jQuery with the following modified U-turn method ( found here ), which will replace the element with children:
$.fn.myUnwrap = function() { this.parent(':not(body)').each(function(){ $(this).replaceWith( this.childNodes ); }); return this; };
And then it would be easy to use it:
$('#my_span').myUnwrap();
source share