Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>jQuery.replaceTagName</h1> <p>The following is a jQuery plugin to replace the tag name of DOM elements.</p> <h2>Source</h2> <pre><code>(function($) { $.fn.replaceTagName = function(replaceWith) { var tags = [], i = this.length; while (i--) { var newElement = document.createElement(replaceWith), thisi = this[i], thisia = thisi.attributes; for (var a = thisia.length - 1; a &gt;= 0; a--) { var attrib = thisia[a]; newElement.setAttribute(attrib.name, attrib.value); }; newElement.innerHTML = thisi.innerHTML; $(thisi).after(newElement).remove(); tags[i] = newElement; } return $(tags); }; })(window.jQuery); </code></pre> <h3>Minified Source</h3> <pre><code>(function(e){e.fn.replaceTagName=function(t){var n=[],r=this.length;while(r--){var i=document.createElement(t),s=this[r],o=s.attributes;for(var u=o.length-1;u&gt;=0;u--){var a=o[u];i.setAttribute(a.name,a.value)}i.innerHTML=s.innerHTML;e(s).after(i).remove();n[r]=i}return e(n)}})(window.jQuery); </code></pre> <h2>Usage</h2> <p>Include the above minified source in your javascript after jQuery.</p> <p>Then you can use the plugin like this:</p> <pre><code>$('div').replaceTagName('span'); // replace all divs with spans </code></pre> <p>Or in your case this:</p> <pre><code>$('td').replaceTagName('th'); </code></pre> <p>jQuery selectors work as expected</p> <pre><code>$('.replace_us').replaceTagName('span'); // replace all elements with "replace_us" class with spans $('#replace_me').replaceTagName('div'); // replace the element with the id "replace_me" </code></pre> <h2>More resources</h2> <p><a href="http://jsfiddle.net/xkt5q/2/">jsFiddle with Qunit tests</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload