To replace a DOM element with the specified HTML or DOM elements using jQuery, use the replaceWith() method. The replaceWith (content) method replaces all matched elements with the specified HTML or DOM elements. This returns the JQuery element that was just replaced, which has been removed from the DOM.
We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.
The replace() is the method that is used to replace the user contents on the application that is it converts one type of value to another type with the data type like string, number, text, etc.
You need to use a regular expression, so that you can specify the global (g) flag: var s = 'some+multi+word+string'. replace(/\+/g, ' ');
Could you refine your selector in the load() method?
For example,
$("#secondHeader").load("/logged-in-content.html #secondHeader > *");
This way, you're not grabbing the div itself, you're grabbing its contents.
I think the best way is to use get instead of load. In your case you can do like this:
$.get("/logged-in-content.html #secondHeader", function(data) {
$("#secondHeader").replaceWith(data);
});
[Edit: removed a paren]
Update: If /logged-in-content.html has more than just the code you need, you can wrap the returning data in another jQuery object and use .find() to extract the container. Try this:
$("#secondHeader").replaceWith($(data).find("#secondHeader"));
Another way that worked best for me:
$('#div_to_replace').load('/ajax/loader', function() {
$(this).children(':first').unwrap();
});
Final Answer:
$.fn.loadWith = function(u){var c=$(this);$.get(u,function(d){c.replaceWith(d);});};
$("#test").loadWith("somelink.html");
jQuery load adds the response INTO the element selected. jQuery's replaceWith REPLACES the selected element.
<div id="curElement">start</div>
$("#curElement").load("somelink.html");
will result in:
<div id="curElement">What ever was in somelink.html</div>
$("#curElement").replaceWith("somelink.html");
will result in:
What ever was in somelink.html
I suggest adding a function to jQuery that does both:
$.fn.loadWith = function(u){
var c=$(this);
$.get(u,function(d){
c.replaceWith(d);
});
};
$("#test").loadWith("somelink.html");
Using $.get() worked for me but I had to extract the container from the response document first:
$.get("/page.html", function (data) {
var elem = $(data).find('#container');
$("#puthere").replaceWith(elem);
});
I always have a jQuery function defined like this:
jQuery.fn.loadOuter = function( url, callback )
{
var toLoad = $(this);
$.get(url, function( data ) {
toLoad.replaceWith( data );
if (callback != null && callback != undefined)
callback();
});
}
Then I can either say
$(...).load(url)
or
$(...).loadOuter(url)
The second one does what you want to do. I also have a function called loadInner which just calls load for what its worth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With