jQuery: how to change tag name?
For example:
<tr>
$1
</tr>
I need
<div>
$1
</div>
Yes, I can
- Create DOM element <div>
- Copy tr content to div
- Remove tr from dom
But can I make it directly?
PS:
$(tr).get(0).tagName = "div";
results in DOMException
.
To replace the internal contents of multiple tags, each with their own original content, you have to use
.replaceWith()
and.html()
differently:http://jsfiddle.net/kcrca/VYxxG/
JS to change the tag name
Related fiddle is in this link
Yet another script to change the node name
http://jsfiddle.net/rc296owo/5/
It will copy over the attributes and inner html into a new element and then replace the old one.
You could go a little basic. Works for me.
Since
replaceWith()
didn't work for me on an element basis (maybe because I used it insidemap()
), I did it by creating a new element and copying the attributes as needed.