The Wayback Machine - https://web.archive.org/web/20231031025336/https://www.geeksforgeeks.org/how-to-replace-innerhtml-of-a-div-using-jquery/
Open In App
Related Articles

How to replace innerHTML of a div using jQuery?

Improve Article
Improve
Save Article
Save
Like Article
Like

For replacing innerHTML of a div with jquery, html() function is used.

Syntax:

$(selector).html()

Example:




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <title>
      changing-innerhtml-using-jquery
  </title>
    <link href=
          rel="stylesheet">
    <style>
        body {
            margin-top: 5%;
        }
          
        div {
            text-align: center;
        }
    </style>
    <script src=
            type="text/javascript">
  </script>
</head>
  
<body>
  
    <div class="div_1">
        <h1>Geeksforgeeks</h1>
        <h1 style="color: green">
          This is the content inside the div before changing
      </h1>
    </div>
    <div class="div_2">
        <button class="btn btn-primary"
                type="submit">
          Click here for changing innerHTML
      </button>
    </div>
  
</body>
<script>
    $(document).ready(function() {
        $('.btn').click(function() {
            $("div.div_1").html(
              "<h1><span style='color: green;'>GeeksforGeeks"+
              "</span><br>This is the content inside the div"+
              " after changing innerHTML</h1>");
        })
  
    })
</script>
  
</html>


After loading the web page, on clicking the button content inside the div will be replaced by the content given inside the html() function.
Output:

Before clicking button:

After clicking button:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Apr, 2019
Like Article
Save Article
Similar Reads
Related Tutorials