[Javascript] accessing a <label>

Mike Dougherty mdougherty at pbp.com
Mon Jun 4 07:42:47 CDT 2007


On 6/3/07, diego nunes <dnunes at gmail.com> wrote:
>     If using the label right before/after the <input> a good solution
> would do a search for a label in the previousSibling/nextSibling,
> comparing the "for" attribute. It wouldn't spend as much time as a
> iteration through the whole getElementsByTagName('label') would.

In that case, I would agree with Nick that it makes as much sense to
just put the control inside the label tag.  It would be easier to get
the control by id, then go to it's parentNode to get to the label than
to walk the DOM carefully ignoring whitespace siblings depending on
browser.  Although once you have a decent library for cross-browser
DOM manipulation, it (probably) would be easier than iterating every
label on the form.  That's why I say it depends on use what the best
solution would be.  It gets even more tricky if you're trying to
develop a solution to be used against dynamic markup.



More information about the Javascript mailing list