I'm trying to display form inputs using javascript. so I used this JS code block
<html>
<head>
</head>
<body>
<script>
var text = document.form1.name.value;
function printText(){
document.write(text);
}
</script>
<form name=form1>
<input type="text" id="txt" name="name" ><br><br>
<input type="button" value="Submit!" onClick="printText();" >
</form>
</body>
</html>
but above code is not working. its giving me to undifined output
but when I'm trying below code its working....
<html>
<head>
</head>
<body>
<script>
function printText(){
var text = document.form1.name.value;
document.write(text);
}
</script>
<form name=form1>
<input type="text" id="txt" name="name" ><br><br>
<input type="button" value="Submit!" onClick="printText();" >
</form>
</body>
</html>
so my problem is why we cant use this var text = document.form1.name.value;
statement outside of function?
Thanks.