We can use HTML onclick attribute to change HTML content. 

The onclick attribute fires on a mouse click on the element. All we need to do is execute a JavaScript when a button is clicked.

onclick attribute Example


<p id="example">JavaScript</p>

<button type="button" onclick='document.getElementById("example").innerHTML = "Hello JavaScript!"'>Click Me to Change the HTML!</button>

Example 2


<p id="example" onclick="changeColor()">Click to change text color.</p>

<script>
function changeColor() {
  document.getElementById("example").style.color = "green";
}
</script>

0 Comments

Leave a reply

Your email address will not be published. required fields are marked *