Dorward

Name that Field

20 April 2007

Yesterday I was working on a form handler and needed to check the names of a number of fields in the (rather long) form. I could have poked at the source or used a DOM Inspector, but (for various reasons) it was easier to use a script.

I threw this together in the space of a couple of minutes and ran it through Firebug. Each field was then prefixed by its name and I had a quick reference while I built the rest of the handler.


function label(el) {
  for (var i = el.length - 1; i > -1; i--) {
    var b = document.createElement('b');
    var text = document.createTextNode(el[i].name);
    b.appendChild(text);
    el[i].parentNode.insertBefore(b, el[i]);
  }
}
label(document.getElementsByTagName('input'));
label(document.getElementsByTagName('select'));
label(document.getElementsByTagName('textarea'));

Feel free to use it if you think it will help you.