puchu puchu - 1 year ago 242
jQuery Question

jQuery: how to change tag name?

jQuery: how to change tag name?

For example:


I need


Yes, I can

  1. Create DOM element <div>

  2. Copy tr content to div

  3. Remove tr from dom

But can I make it directly?


$(tr).get(0).tagName = "div";

results in

Answer Source

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:

$('#target').replaceWith('<newTag>' + $('target').html() +'</newTag>')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download