/ Published in: JavaScript
A JS function that put the focus in the form field associated to a label element whenever is clicked. The for atributte of the label element must match the id attribute of the associated form field.
It takes no arguments.
From book Dom Scripting by Jeremy Keith
It takes no arguments.
From book Dom Scripting by Jeremy Keith
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function focusLabels() { if (!document.getElementsByTagName) return false; var labels = document.getElementsByTagName("label"); for (var i=0; i<labels.length; i++) { if (!labels[i].getAttribute("for")) continue; labels[i].onclick = function() { var id = this.getAttribute("for"); if (!document.getElementById(id)) return false; var element = document.getElementById(id); element.focus(); } } }