Return to Snippet

Revision: 1900
at November 20, 2006 09:17 by gaunab


Updated Code
jQuery.fn.replaceWith = function(replacement) {
  return this.each(function(){
    element = $(this);
    $(this)
      .after(replacement).next()
      .attr('class', element.attr('class')).attr('id',element.attr('id'))
      .html(element.html())
      .prev().remove();
  });
};

/* 
usage example

$('a#fooid').replaceWith('<span></span>');

before:
<a id="fooid" class="whatever">some text</a>

after:
<span id="fooid" class="whatever">some text</span>

*/

Revision: 1899
at November 20, 2006 08:49 by gaunab


Initial Code
jQuery.fn.replaceWith = function(replacement) {
  return this.each(function(){
    element = $(this);
    $(this)
      .after(replacement).next()
      .attr('class', element.attr('class')).attr('id',element.attr('id'))
      .html(element.html())
      .prev().remove();
  });
};

/* usage example */

$('a#fooid').replaceWith('<span></span>');

/* before: */
<a id="fooid" class="whatever>some text</a>
/* after: */
<span id="fooid" class="whatever>some text</span>

Initial URL


Initial Description
Replaces a DOM Element with another, but keeps the classes and IDs of the old one.

Initial Title
JQuery plugin: Replace DOM element and keep classes and id

Initial Tags
jquery

Initial Language
JavaScript