JSFiddle - React, Tailwind, and code Playground

by Michel Plungjan

HTML

<form id="theForm">
<input type="text" name="foo" value="one">
<input type="text" name="foo" value="two">
</form><br />
<div id="messages"></div>

JavaScript

function logIt(str) {
  document.getElementById('messages').innerHTML+='<br/>'+str
}    
var form = document.getElementById("theForm");
var formFoo = form["foo"];
var index;
logIt("typeof formFoo = " + typeof formFoo);
if (typeof formFoo !== "undefined") {
  logIt("Object#toString says: " + Object.prototype.toString.call(formFoo));
}
if ('length' in formFoo && 'item' in formFoo) {
  logIt("Looks like a collection of some kind:");
  for (index = 0; index < formFoo.length; ++index) {
    logIt(index + ": " + formFoo[index].value);
  }
}
logIt('<hr/>');
var docFoo = document.getElementsByName("foo");
logIt("typeof docFoo = " + typeof docFoo);
if (typeof docFoo !== "undefined") {
  logIt("Object#toString says: " + Object.prototype.toString.call(docFoo));
}
if ('length' in docFoo && 'item' in docFoo) {
  logIt("Looks like a collection of some kind:");
  for (index = 0; index < docFoo.length; ++index) {
    logIt(index + ": " + docFoo[index].value);
  }
}