(this used to be a blog)

jQuery: What to do when replaceWith() doesn’t work

Posted: October 20th, 2008 | Author: Panos Karageorgakis | Filed under: Development, Web, jQuery | Tags: , , | 1 Comment »

Nothing’s perfect in this world and jQuery is not the exception to prove the rule. Sometimes the replaceWith() function doesn’t seem to work, no matter what you do. It looks like the function does nothing, and that’s a problem. But don’t bother; this will do the trick:

instead of

$('#someElement').replaceWith('something');

use this:

$('#someElement').empty().append('something');
Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

One Comment on “jQuery: What to do when replaceWith() doesn’t work”

  1. 1 Jeff said at 9:18 pm on January 13th, 2009:

    This helped me out a lot!

    I was trying to this:
    $(“.vnl-box4-content”).fadeOut(500, function () {
    //$(this).replaceWith(“” + productDESC + “”);
    $(this).empty().append(productDESC);
    $(“.vnl-box4-content”).fadeIn(“slow”);

    The problem was, .fadeIn just wasn’t working. I figured out it was because .replaceWith was making the hidden content show.

    Your method keeps the box hidden, while still replacing the content inside the box :)

    Thanks Panos!


Leave a Reply