Forum Moderators: open
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form action="#">
<label for="fname">First name</label><input type="text" id="fname">
<br>
<label for="lname">Last name</label><input type="text" id="lname">
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample Form</title>
<style type="text/css">
.focused {
background-color:#fdb;
}
</style>
<script type="text/javascript">
function init(){
inp=document.getElementsByTagName('input');
for(c=0;c<inp.length;c++){
inp[c].onfocus=function() {
if(this.previousSibling.nodeName=='LABEL'){
this.previousSibling.className='focused';
this.onblur=function() {
this.previousSibling.className='';
}
}
}
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<form action="#">
<label for="fname">First name</label><input type="text" id="fname">
<br>
<label for="lname">Last name</label><input type="text" id="lname">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.focused {
background-color: #fdb;
}
</style>
<title>Sample Form</title>
</head>
<body>
<form action="#">
<div>
<div><label for="fname">First name</label><input type="text" id="fname"></div>
<div><label for="lname">Last name</label><input type="text" id="lname"></div>
</div>
</form>
<script>
(function () {
var i,
j,
inputEls = document.getElementsByTagName('input'),
labelEls = document.getElementsByTagName('label');
function attachFocusHandler(inputEl, labelEl) {
inputEl.onfocus = function () {
labelEl.className = labelEl.className + ' focused';
};
inputEl.onblur = function () {
labelEl.className = labelEl.className.replace('focused', '');
};
}
for (i = 0; i < inputEls.length; i++) {
for (j = 0; j < labelEls.length; j++) {
if (inputEls[i].id === labelEls[j].getAttribute("for")) {
attachFocusHandler(inputEls[i], labelEls[j]);
break;
}
}
}
})();
</script>
</body>
</html>
[edited by: Fotiman at 1:43 pm (utc) on Jun 8, 2012]