How do you replace a tag in html?

JavaScript is widely used in web page development and its libraries like JQuery are very handy. In old technologies, it was nearly impossible to change or replace an HTML element using server-side scripting languages, we were only able to change the content inside the element through the backend languages. But thanks to JavaScript, it made life easier and allowed us to dynamically change the element of the webpage.

It provides various methods to achieve this functionality but the one we are going to look at in this post is the JQuery’s replacewith() method.

Let’s take a deep look at this method.

In jQuery, the replaceWith() function is used to replace chosen components with new ones and it returns the components that have been replaced.

The replaceWith() method syntax is mentioned below.

$(element).replaceWith(newContent, (idx)=>{})

Arguments Explanation

Here’s the explanation of the arguments of replacewith() method

  • newContent: It is the content that will be used to replace the parts that have been chosen.
  • (idx)=>{}: It’s the function that returns the replacement content. It also has an index of arguments and this index parameter is used to return the index position of the element.

To better understand how to utilize the replaceWith() function, consider the following examples.

Example #1

Suppose, we have a

element in our HTML whose id is “element1” and at the click of a button we want to replace it with the

tag.

The HTML part would be like this:

And at the click of a button, we want to replace the

Now, to completely convert the

tag and its content to the

tag, jQuery’s code will go like this:

$(document).ready(function(){

$("#btn").click(function() {

$("#element1").replaceWith("

Element Replaced :)

");

});

});

In the above example, we are simply first fetching the HTML element by using the IDs of the button and div tag and then replacing the

tag with the

tag using replaceWith() method.

Output

You can witness that the element was successfully replaced.

Conclusion

JavaScript’s replacewith() method is used to replace one HTML element with some other element according to the requirement. This replacewith() method takes two arguments, the first argument is the content that you want to be replaced with while the second argument is used to return the index position of the content that you want to replace with. In this article, we have discussed the examples of replacing the old content with the new one using JQuery.

About the author

How do you replace a tag in html?

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Document Object Model (DOM) is a platform and language-neutral interface which is used by programs and scripts to dynamically access the content, style and structure. Please refer to DOM (Document Object Model) to know the details. We can implement this by using DOM’s createElement(), createTextNode(), appendChild(), replaceChild() methods and childNodes property.

    Let us discuss these methods and property to replace one HTML element to another.

    • createElement(): It is used to create an element node with the specified name.
      Syntax:
      var element = document.createElement("Element_name"); 
      

      In this example, the element is “h2” tag, so write

       var element=document.createElement("h2");
    • createTextNode(): The method is used to create a text node.
      Syntax:
       var txt = document.createTextNode("Some_Text");
    • appendChild(): After creating text node, we have to append it to the element by using appendChild() method.

      Syntax:

       element.appendChild(Node_To_append);

    The following shows the working code on how to use these methods and property discussed above.

    HTML

    <html>

    <body>

        <h2>

            Using DOM'S to print Hello World 

        h2>

        <script>

            var h = document.createElement("h3");

            var txt = document

                .createTextNode("Hello World!!");

            h.appendChild(txt);

            document.body.appendChild(h);

        script>

    body>

    html>

    Output:

    childNodes[Position]: This property returns a collection of child nodes as a NodeList object. The nodes are sorted as they appear in source code and can be accessed by index number starting from 0.

    replaceChild(): It replace a child node with new node.

    old_Node.replaceChild(new_Node, old_node.childNodes[node_position]);

    Example: The following code shows how to replace element with another one.

    HTML

    <html>

    <body>

        <div id="p1">

            <p id="red">Hello World p>

            <button onclick="repFun()">

                Click Me

            button>

        div>

        <p id="demo">p>

        <script>

            function repFun() {

                // Creating "h2" element

                var H = document.createElement("h2");

                // Retaining id  

                H.setAttribute("id", "red");

                // Creating Text node        

                var txt = document.createTextNode("Hello Geeks!!");

                // Accessing  the Element we want to replace 

                var repNode = document.getElementById("p1");

                // Appending Text Node in Element  

                H.appendChild(txt);

                // Replacing one element with another

                p1.replaceChild(H, p1.childNodes[0]);

            }

        script>

    body>

    html>

    Output:


    How do you change an element tag?

    To change the element tag name in JavaScript, simply need to make a new element and move over all the elements so you keep onclick handlers and such, and then replace the original thing.

    How do you replace part of a string in HTML?

    The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.

    How do I remove a tag from a string?

    The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.