Check out the error console in your browser, and you'll see the problem:
TypeError: list[1] is undefined
As soon as you use
document.write
, the entire contents of the document are replaced with whatever you've written.
Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which will clear the document.
Clearing the document destroys the elements on it, so you can no longer read the value of the inputs.
You'll need to read the values
before writing to the document:
function fun(){
var list = document.getElementsByName("array");
var values = [];
for (var i = 0; i < list.length; i++){
values.push(list[i].value);
}
document.write(values.join("<br />"));
document.close();
}