jQuery: What to do when replaceWith() doesn’t work
Posted: October 20th, 2008 | Author: Panos Karageorgakis | Filed under: Development, Web, jQuery | Tags: jQuery, replace, trick | 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');
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!