null return by getElementById method in JavaScript
<html>
<head>
<title>test_elementObject</title>
<script language="JavaScript" type="text/javascript">
<!--
var input1 = document.getElementById ( "input1" );
input1.value = "sample text"
//-->
</script>
</head>
<body>
<input id = "input1" type = "text" value = "" />
</body>
</html>
I’m new in JavaScript
why the script cause error: “input1 is null”?
It happened because this code is executed before the body content is loaded. Try this code:
This command makes the code execute only when all the page content is loaded
And remember that
<!--and//-->are outdated, due to javascript compatibility on all recent browsers