Jquery replace() Vs replacewith()

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


6 points

Hello,

What is the difference between jquery replace() and Jquery replacewith()?

Mark



8 points

Jquery replaceWith is used to replace a text or HTML content with another one.

The syntax runs as follows:

.replaceWith( newContent )

Jquery replace allows us to "find" using regular expression and then replace with the new content.

Syntax for jquery replace() is:

.replace(<regular expression>, "newContent");

As an example to jquery replacewith():

<div class="container">
  <div class="inner first">Hello</div>
  <div class="inner second">And</div>
  <div class="inner third">Goodbye</div>
</div>

$('.second').replaceWith('<h2>New heading</h2>');

would give output:

<div class="container">
  <div class="inner first">Hello</div>
  <h2>New heading</h2>
  <div class="inner third">Goodbye</div>
</div>

For example on Jquery find and replace, click on the link.

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.