jQuery replaceWith find new element jQuery replaceWith find new element jquery jquery

jQuery replaceWith find new element


$.fn.replaceWithPush = function(a) {    var $a = $(a);    this.replaceWith($a);    return $a;};

See a working demo


I believe replaceAll() returns the new content. It has the target and source reversed from replaceWith().

var newobj = $( "<p>New paragraph" ).replaceAll( "#replaceme" );


Precede your call to replaceWith() with a jQuery call to find the parent node and then use the parent to find the new node that was inserted with the replaceWith() function.

Here's an example inside the success handler of a $.ajax() function that replaces a form:

success: function(data,textStatus,jqXHR){    var $li=$form.parents('li');            $form.replaceWith(data);    var $form=$li.find('form');    //...},

In this example, there was a unique form for each li. You may need to tailor your approach depending on how you construct your DOM.