Click Home >Web Front-end >HTML Tutorial >Execute script when HTML element is clicked? Use the onclick attribute to execute a script when an element is clicked in HTML. The above is the detailed content of Execute script when HTML element is clicked?. For more information, please follow other related articles on the PHP Chinese website!Execute script when HTML element is clicked?
Example
You can try running the following code to implement the onclick attribute -<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Click</button>
<p id = "test"></p>
<script>
function display() {
document.getElementById("test").innerHTML = "You clicked the button!";
}
</script>
</body>
</html>