Hey, JavaScripters!

You know what kind of annoys me?

I mean, besides the post office?

If you want to make a super accessible form, with labels for every field, it’s a pain in the unowaht to access those labels with javascript. I thought, Wouldn’t it be nice if there was a way to access all the labels by their for attribute with a handy top-level object, much like you can access the fields in a form by their id and name attributes?

This is a handy little function to create a top-level object access all the labels by their for attribute. Stick these two functions somewhere in your script, and then then reference the label by document.getLabelByFor({the FOR attrib value}).

So, if you had: <label for="fieldid">Field</label>
<input name="fieldname" id="fieldid" type="text" value="field value" />
then document.getLabelByFor('fieldid') would return a reference to the label element.

Read on for the code snippets…

Put this somewhere in your script to use the function:

//make array of label ids, referenced by their FOR attribs
function loadLabels(){
  var l=document.getElementsByTagName('label');
  if(!document.labels)document.labels=new Object;
  var baseId=String('labels'+
  var theFor='';
  for(var i=l.length-1;i>-1;i--){
    else if(l[i].getAttribute(’for’))
    else if(l[i].getAttribute(’htmlFor’))
    else theFor=”;
  if(!document.labels||!document.getElementById)return null;
  return document.getElementById(document.labels[strFor]);

Going a little further, if you create a special CSS class to hilight required values, you could have a few handy functions that set certain fields to required based on user input. Just send these functions the field element, and they’ll add/remove “required” and “disabled” classes to/from the associated label:

function requireField(lnk){
  unDisableField(lnk); //don't disable it if it's required!
function unRequireField(lnk){

function disableField(lnk){
  unRequireField(lnk); //don't require if it's disabled!
function unDisableField(lnk){

These functions are super-handy and I use them a lot. Basically just a quick little way to add or remove a class if you’re using several classes in a situation.

function addClass(lnk,cls) {
  else if(lnk.className.indexOf(cls)!=-1)return;
  else lnk.className+=' '+cls;
function remClass(lnk,cls) {
  else if(!cls)lnk.className='';
  else if(lnk.className.indexOf(cls)!=-1)

This bit I picked up online, and have used a bajillion times since. Works like the old c and php str_replace(), and it’s flawless.

// by Real Gagnon
function replace(haystack,needle,replacement) {
  var i=haystack.indexOf(needle);
  if(i==-1)return haystack;
  return r;

Leave a Reply

Comments are moderated like crazy using a variety of plugins. There is a very high likelihood that your comment won't show up right away, especially if you have never commented here before, but it was not deleted.

Please be patient, and do not post your comment more than once. It will show up once it is approved.

You must be logged in to post a comment.