I'm working on a JavaScript function that is supposed to display the text entered into an input field. Here is the code I have written:
<head>
<script type="text/javascript">
function showText()
{
var input = document.body.childNodes[1];
document.write(input.value);
}
</script>
</head>
<body>
<input type="text"/>
<input type="button" onclick="showText()"/>
</body>
One thing that confuses me is why I need to use the index "1" in the childNodes array. It seems like it should be "0" since <input type="text"/>
is the first child of the body element.