[thelist] Reading out the "for" atttribute via Javascript

Chris Heilmann lists at onlinetools.org
Thu Feb 24 11:39:23 CST 2005


This just got me confused for 20 minutes, hopefully it'll be of help for
someone here

<tip type="DOM Javascript" author="Chris Heilmann">
When reading out the "for" attribute of a label element, IE and Gecko part
ways - once again.
Firefox grabs yourobject.getAttribute('for'), whereas IE needs
yourobject.getAttribute('htmlFor').
Both (and Opera) can use:
var
labelfor=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');

An example:
labels=document.getElementsByTagName('label');
for(var i=0;i<labels.length;i++)
{
var
labelfor=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
fieldnames[labelfor]=labels[i].firstChild.nodeValue;
}
</tip>
-- 
Chris Heilmann
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/
Binaries: http://www.onlinetools.org/



More information about the thelist mailing list