-
-
Save haraldmartin/154597 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In most browsers, a <label> tag around a form field, e.g.: | |
// | |
// <label> | |
// <input type="checkbox" id="private" name="private"> | |
// This message is private | |
// </label> | |
// | |
// works just as if you'd specified a for= attribute on the label | |
// for the first visible field inside the label. In Safari 4, this | |
// seems not to be the case (i.e. the for= attribute is required). | |
// | |
// The following code watches the page for all clicks on <label>s | |
// and automatically sets the for= attribute, if not present, to | |
// the first visible form field. | |
document.observe("mousedown", function(event) { | |
var label = event.findElement("label"); | |
if (label && !label.hasAttribute("for")) { | |
var element = label.down("input[type=radio], input[type=checkbox], input[type=submit]," + | |
"input[type=text], input[type=password], select, textarea"); | |
if (element) { | |
label.writeAttribute("for", element.identify()); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment