I have a very simple html file that contains an input
element. Whenever the input
element is focused on, I want to console.log
a message. What happens is, the console.log is fired immediately upon the loading of the page, and never again. I can't use inline onfocus
and no jquery. Here is what I've tried.
first
<html>
<body id="fname" >
<input id="fname" type="text" >
</body>
<script>
window.onload = () => document.getElementById("fname").addEventListener(`onfocus`,console.log(`hi`));
</script>
</html>
second
<html>
<body id="fname" >
<input id="fname" type="text" >
</body>
<script>
window.onload = () => document.getElementById("fname").onfocus = console.log(`hi`);
</script>
</html>
What gives?