jQuery: how to change tag name?
For example:
JavaScript
x
4
1
<tr>
2
$1
3
</tr>
4
I need
JavaScript
1
4
1
<div>
2
$1
3
</div>
4
Yes, I can
- Create DOM element <div>
- Copy tr content to div
- Remove tr from dom
But can I make it directly?
PS:
JavaScript
1
2
1
$(tr).get(0).tagName = "div";
2
results in DOMException
.
Advertisement
Answer
You can replace any HTML markup by using jQuery’s .replaceWith()
method.
example: http://jsfiddle.net/JHmaV/
Ref.: .replaceWith
If you want to keep the existing markup, you could use code like this:
JavaScript
1
2
1
$('#target').replaceWith('<newTag>' + $('#target').html() +'</newTag>')
2