A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to Replace innerHTML of a Div using jQuery
Q:

How to Replace innerHTML of a Div using jQuery

0

How to Replace innerHTML of a Div using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.5.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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now