/

HTML / CSS / JavaScript Tutorial

HTML Event attribute: onclick

[this page | pdf | back links]

The HTML onclick attribute specifies the event that is triggered when an element is clicked (mouse clicked). It applies to all visible elements.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
onclick: try clicking on this
<span style="background-color: yellow;" id="idOnclick" onclick="myOnclick()">element</span>
(it should become red)<br>

<script>
function myOnclick() {document.getElementById("idOnclick").style.color = "red";}
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Attributes


Desktop view | Switch to Mobile