I am trying to attach a click or onlclick event to a button element through external JavaScript but it is not working. Can anyone help? I am writing my code below for reference. Html part:-
<head>
<script src="script.js"></script>
</head>
<body>
<button id="myBtn">ClickMe</button>
<p id="demo"></p>
</body>
JS code :-
document.getElementById("myBtn").addEventListener("click", myFunction);
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}