views:

2456

answers:

5

Let's say I have an html form. Each input/select/textarea will have a corresponding <label> with the for attribute set to the id of it's companion.

Given an input element in javascript --via an onkeyup event, for example--, what's the best way to find it's associated label?

+1  A: 

have you tried using document.getElementbyID('id') where id is the id of the label or is the situation that you dont know which one you are looking for

jmein
I know the id of the input element, but not the id of the label.
Joel Coehoorn
can you not set a corresponding id for the label such as input id='test' and label id='lbl_test"
jmein
+6  A: 

Earlier...

var labels = document.getElementsByTagName("LABEL");
var lookup = {};
for (var i = 0; i < labels.length; i++);
{
  lookup[labels[i].htmlFor] = label;
}

Later...

var myLabel = lookup[myInput.id];

Snarky comment: Yes, you can also do it with JQuery. :-)

Tomalak
Nice. I'll refactor that just a little so that it builds the lookup the first time I call the method, but it should do the trick.
Joel Coehoorn
I implied that you would build the lookup only once.
Tomalak
huh did not think of that +1
jmein
Be aware that there was a small error in it: Use the "htmlFor" property, not "for", as the latter is a reserved word in JS. I tend to forget that when using label objects... :-)
Tomalak
I've got a way to do this without a lookup array above.
FlySwat
I meant move the init code inside the method, not do it just once :(
Joel Coehoorn
You did not mention any "method", and currently I'm not even sure if I know what you refer to as "init code". All I was saying is that for one page the look-up object needs to be built only once. *Where* and *when* you do it is entirely up to you.
Tomalak
I thought my use of the words "earlier" and "later" would have made it clear that the former would happen on document load, and the latter would happen anytime you need it.
Tomalak
+7  A: 

If you are using jQuery you can do something like this

$('label[for="foo"]').hide ();

If you aren't using jQuery you'll have to search for the label. Here is a function that takes the element as an argument and returns the associated label

function findLableForControl(el) {
   var idVal = el.id;
   labels = document.getelementsByTagName('label');
   for( var i = 0; i < labels.length; i++ ) {
      if (labels[i].htmlFor == idVal)
           return labels[i];
   }
}
TonyB
+1  A: 

with jquery you could do something like

var nameOfLabel = someInput.attr('id');
var label = $("label[for='+nameOfLabel+']");
AndreasKnudsen
+7  A: 

First, scan the page for labels, and assign a reference to the label from the actual form element:

var labels = document.getElementsByTagName('LABEL');
for (var i = 0; i < labels.length; i++) {
    if (labels[i].htmlFor != '') {
         var elem = document.getElementById(labels[i].htmlFor);
         if (elem)
            elem.label = labels[i];   
    }
}

Then, you can simply go:

document.getElementById('MyFormElem').label.innerHTML = 'Look ma this works!';

No need for a lookup array :)

FlySwat
Can you explain where your use of an expando property differs from mine? I do not use any "lookup array", I use an object with properties. There is only syntactical difference between "element.label", "element['label']" or "lookup['elementId']". Behind the scenes they are *the same thing*.
Tomalak
This is nicer because I don't have to keep a separate object around- the relationship is stored with the input element directly.
Joel Coehoorn