Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

onclick alert javascript

<!-- Best practice -->
<button id="btn1">Click me!</button>
<script>
	document.getElementById("btn1").addEventListener("click", function() {
		window.alert("You clicked me!");
	});
</script>
<!-- Still valid -->
<button id="btn2">Click me!</button>
<script>
	document.getElementById("btn2").onclick = function() {
		window.alert("You clicked me!");
	};
</script>
<!-- A different version of the above -->
<button id="btn3" onclick="btn3ClickHandler()">Click me!</button>
<script>
	function btn3ClickHandler() {
		window.alert("You clicked me!");
	}
</script>
 
PREVIOUS NEXT
Tagged: #onclick #alert #javascript
ADD COMMENT
Topic
Name
4+3 =