8

How to Replace innerHTML of a Div using jQuery

 1 year ago
source link: https://www.laravelcode.com/post/how-to-replace-innerhtml-of-a-div-using-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Replace innerHTML of a Div using jQuery

  3490 views

  2 years ago

jQuery

Use the jQuery html() Method

You can simply use the jQuery html() method to replace innerHTML of a div or any other element.

The jQuery code in the following example will replace the innerHTML of a DIV element having id pageTitle with an HTML heading on click of the button. Let's try it out:

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Replace innerHTML of a Div</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#pageTitle").html("<h1>Hello, World!</h1>");
    });
});
</script>
</head>
<body>
    <div id="pageTitle">
        <p><b>Click the following button to replace me.</b><p>
    </div>
    <button type="button">Replace HTML</button>
</body>
</html>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK