That all said, here is a better version. But it relies heavily upon browser sniffing, so will most likely miss some of those I can't test in, and become outdated as browsers become more standardized. It may also cause problems in some browsers, use it at your own risk:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Highlight Form Elements - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
//Highlight form element- © Dynamic Drive (www.dynamicdrive.com)
//For full source code, 100's more DHTML scripts, and TOS,
//visit http://www.dynamicdrive.com/ This notice must remain for legal use.
//Updated 29/May/2008 in http://www.dynamicdrive.com/forums by username:jscheuer1
highlight.color="yellow";
//Function to highlight form element
function highlight(obj){
var h = highlight, b = function(el, v){
el.style.backgroundColor = v;
if (!h.ie && !window.opera && el.type &&
/(radio)|(checkbox)/.test(el.type.toLowerCase()) &&
el.parentNode.tagName.toLowerCase() == 'span')
el.parentNode.style.backgroundColor = v;
};
if (h.p != '')
b(h.p, '');
b(obj, h.color);
h.p = obj;
}
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
highlight.ie = true;
@end @*/
highlight.p = '';
if (document.getElementById)
window.onload=function(){
for (var a = function(){highlight(this);return true;},
f = document.forms, i = f.length-1; i > -1; --i)
for (var e = f[i].elements, j = e.length-1; j > -1; --j)
if (window.opera && e[j].tagName && e[j].tagName.toLowerCase() == 'select')
e[j].onmouseover = e[j].onfocus = function(){this.focus();highlight(this);return true;};
else if (!highlight.ie && !/Apple/.test(navigator.vendor))
e[j].onfocus = a;
else {
if(/Apple/.test(navigator.vendor)&&e[j].type&&e[j].type.toLowerCase()=='text'){
e[j].style.border = '1px solid #ccc';
e[j].style.borderTopColor = e[j].style.borderRightColor = '#333';
}
e[j].onmousedown = e[j].onkeydown = a;
}
}
</script>
</head>
<body>
<form action="">
<div>
<select name="">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select><br>
<span><input type="radio" name="bob" value=""></span>
<span><input type="radio" name="bob" value=""></span><br>
<span><input type="checkbox" name=""></span><br>
<input type="text"><br>
<textarea rows=4 cols=10></textarea><br>
<input type="button" value="Form Button">
</div>
</form>
</body>
</html>
Bookmarks