I am trying to do the simple task of copying the value of the first text boxbox to the second textbox
here is my html
<html>
<head>
<link rel="stylesheet" href="Style.css">
<script src="1.js"></script>
</head>
<body>
textbox1: <input id="textbox1" onkeyup="TextCopy()" type="text"> <br>
textbox2: <input id="textbox2" type="text">
</body>
</html>
here is my js file:
const text1 = document.getElementById("textbox1");
const text2 = document.getElementById("textbox2");
function TextCopy() {
text2.value = text1.value;
}
the error i get is: 1.js:6 Uncaught TypeError: Cannot read properties of null (reading 'value') at TextCopy (1.js:6:25) at HTMLInputElement.onkeyup (1.html:7:73)
if I declare text1 and text2 inside the function I get no errors and it works correctly, so why doesn't it work when I declare them as global variables?
thanks.