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>

function changeColor() {
  document.getElementById("example").style.color = "green";


Leave a reply

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