I have a site I'm working on and it uses 'aside' tags, which I'm not getting IE8 to be able to read no matter what I try, even with an HTML5 Shiv. So, I'm wondering, how would you replace existing tags with other tags with jQuery?
For example, if I wanted to change
<aside>
<h3></h3>
</aside>
to
<div>
<h3></h3>
</div>
How would that be done?
To replace a DOM element with the specified HTML or DOM elements using jQuery, use the replaceWith() method. The replaceWith (content) method replaces all matched elements with the specified HTML or DOM elements. This returns the JQuery element that was just replaced, which has been removed from the DOM.
Using JavaScript The most common approach to replace the content inside an element is with the innerHTML property. Alternatively, you can use the textContent to set the element's text content, which is considered safe against XSS attacks.
Try this:
$('aside').contents().unwrap().wrap('<div/>');
aside
first.unwrap
the contents.wrap
the contents inside a new tag, here a div
.DEMO
Also, you can do this using .replaceWith()
method like:
$('aside').replaceWith(function () {
return $('<div/>', {
html: $(this).html()
});
});
DEMO
$('aside').replaceWith('<div><h3></h3></div>');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With