Questions tagged [replacewith]

JQuery .replaceWith() method. Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.

JQuery .replaceWith() method. Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.

The .replaceWith() method, like most jQuery methods, returns the jQuery object so that other methods can be chained onto it. However, it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.

As of jQuery 1.4, .replaceWith() can also work on disconnected DOM nodes.

Source:http://api.jquery.com/replaceWith/

316 questions
157
votes
5 answers

What's the difference between jQuery's replaceWith() and html()?

What's the difference between jQuery's replaceWith() and html() functions when HTML is being passed in as the parameter?
DMCS
  • 31,720
  • 14
  • 71
  • 104
44
votes
3 answers

Update div with jQuery ajax response html

I am trying to update a div with the content from an ajax html response. I beleive I have the syntax correct, however the div content gets replaced with the entire HTML page response, instead of just the div selected in the html response. What am I…
thedeepfield
  • 6,138
  • 25
  • 72
  • 107
34
votes
2 answers

New Line in Textarea to be converted to

There's a lot of threads here about converting br/> or preserving newlines across different languages, but not many regarding textarea. I have this script: var boxText = ""; $("textarea.BoxText").live('dblclick', function () { boxText =…
C_K
  • 1,243
  • 4
  • 18
  • 29
11
votes
2 answers

how to get actual content of an object after being replaceWith('something') in jquery

I have this code $(document).ready(function(){ $('.selector').click(function(){ obj = $(this); obj.replaceWith('
whats up man ??!
'); alert(obj.html()); }); }); I want to get the new content of…
mdennisa
  • 177
  • 2
  • 13
10
votes
3 answers

best way to write jQuery's replaceWith() in natural JavaScript

Having trouble with a function hitting the page as soon as possible, so i need to write it in pure javascript and include it in the head. not sure how to go about it, because as i understand it, by using .replace() the new element will be moved to a…
technopeasant
  • 7,809
  • 31
  • 91
  • 149
9
votes
4 answers

Using jQuery replaceWith to replace content of DIV only working first time

I'm using the following jQuery to pull new data and replace the contents of the DIV listdata $(function(){ $('.refresh').click(function(event) { event.preventDefault(); $.ajax({ url: "_js/data.php", success:…
Paul
  • 661
  • 4
  • 12
  • 22
9
votes
3 answers

Using $(this) after .replaceWith()

Please consider the below HTML and Javascript. In the script, I am replacing an a tag with a p tag. I am expecting the alert() function to return the contents of the p tag but instead it returns the contents of the original a tag which no longer…
GateKiller
  • 74,180
  • 73
  • 171
  • 204
9
votes
6 answers

Change the tag but keep the attributes and content -- jQuery/Javascript

Text changed to

Text

I'm familiar with jQuery's replaceWith but that doesn't keep attributes/content as far as I know. Note: Why…
Kyle Cureau
  • 19,028
  • 23
  • 75
  • 104
9
votes
3 answers

replace plain-text with html using jQuery

I'm trying to write a replacewith function in jQuery that will replace specific words with html. Basically I'm trying to replace something like [this] with as the document is loaded. I've looked around for a couple samples…
xxstevenxo
  • 661
  • 3
  • 12
  • 27
8
votes
2 answers

Jquery replaceWith - fadeout / Fadein

I think this is going to come across very basic stuff but I'm brand new at the wonderful world of jquery. Heres my simple working replace code :
yes yes
What I am trying to…
Webby
  • 2,655
  • 5
  • 26
  • 34
8
votes
2 answers

replaceWith doesn't work the second time

I need to click on a list item to replace text in a div with text from a node in an xml file. I click on a list item and store the result in a variable. I then load the xml, find the title of the node I need, and make a variable of the result. I…
mweb202
  • 91
  • 2
  • 6
8
votes
3 answers

"jQuery way" to replace just a text node with a mix of HTML and text?

In my web browser userscript project I need to replace just one text node without affecting the other HTML elements under the same parent node as the text. And I need to replace it with more than one node:
foo / bar; baz
hippietrail
  • 15,848
  • 18
  • 99
  • 158
7
votes
4 answers

When doing a .replaceWith() in jQuery the event bindings are not preserved

Here's a condensed example. Keep in mind that the .chat-reply class has a click event handler bound to it. The HTML (Reply Button): This jQuery function is called…
generalopinion
  • 1,437
  • 2
  • 13
  • 21
7
votes
1 answer

BS4 replace_with result is no longer in tree

I need to replace multiple words in a html document. Atm I am doing this by calling replace_with once for each replacement. Calling replace_with twice on a NavigableString leads to a ValueError (see example below) cause the replaced element is no…
Natan
  • 728
  • 1
  • 7
  • 23
6
votes
2 answers

replaceWith on XML problem

After examining the jQuery source, I see that the problem I am having is because replaceWith calls html which does not exist for XML documents. Is replaceWith not supposed to work on XML documents? I have found this admittedly simple workaround, in…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
1
2 3
21 22