In future, please post about Dynamic Drive Scripts in the Dynamic Drive Scripts Help section here where I've moved this.
The script is buggy as regards selects, especially in IE, but also to a lesser extent in others. I'd suggest removing them from the list. Change:
Code:
//Regular expression to highlight only form elements
var intended=/INPUT|TEXTAREA|SELECT|OPTION/
to:
Code:
//Regular expression to highlight only form elements
var intended=/INPUT|TEXTAREA/
As a side note, if onfocus is used instead of onkeyup and onclick, things improve. However, at that point the event must be applied directly to the element, using the form no longer works, so things get complicated. And they are still not ideal, the first click in IE doesn't drop down the list.
But you can try it out if you like. Use this modified version of the script:
Code:
<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
var highlightcolor="green"
var ns6=document.getElementById&&!document.all
var previous=''
var eventobj
//Regular expression to highlight only form elements
var intended=/INPUT|TEXTAREA/
//Function to check whether element clicked is form element
function checkel(which){
if (which.style&&intended.test(which.tagName)){
if (ns6&&eventobj.nodeType==3)
eventobj=eventobj.parentNode.parentNode
return true
}
else
return false
}
//Function to highlight form element
function highlight(e){
eventobj=ns6? e.target : event.srcElement
if (previous!=''){
if (checkel(previous))
previous.style.backgroundColor=''
previous=eventobj
if (checkel(eventobj) || e.type === 'focus')
eventobj.style.backgroundColor=highlightcolor
}
else{
if (checkel(eventobj) || e.type === 'focus')
eventobj.style.backgroundColor=highlightcolor
previous=eventobj
}
}
</script>
And add this onfocus event to any select:
Code:
<select name="selectc" id="selectc" onfocus="highlight(event);">
This approach may have other problems though.
Bookmarks