Forum Moderators: open
Sample code:
<div id="showvalue"></div>
<select name="test">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
If the user clicks this select and moves their mouse over the different choices, can the div be updated to reflect the
valueof the option over which their mouse is hovering? So, before they click, if the mouse is over the "Two" option, div#showvalue would contain "2"
Any ideas for how to search for this solution are welcome as I've had little luck finding other attempts. (I'm pretty sure I'll have to make a "fake" select menu but would prefer to avoid that.)
Thanks! :D
The basic idea is to:
- attach a mouseover event to the <select>
- when called check the target to see if it is an option
- if it is, update the <div>
This should get you started:
<html>
<head>
<script type="text/javascript">
function showSelectValue(e) {
if (e.target.id!= 'select') {
document.getElementById('test').innerHTML = e.target.value;
}
}
function attachTest() {
document.getElementById('select').addEventListener('mouseover',showSelectValue,false);
}
</script>
</head>
<body onload="attachTest()">
<div id="test">Something Here</div>
<select id="select">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
</body>
</html>
rewrite your function attachTest
function attachTest() {
var s=document.getElementById('select');
if(s.attachEvent){
s.attachEvent('onmouseover',showSelectValue);
} else {
s.addEventListener('mouseover',showSelectValue,false);
}
}
e.targetto reference the
<option>s as the mouse moves over them.
e.targetcontinues to reference the
<select>. :(
Adding a line to the
showSelectValuefunction illustrates the issue:
function showSelectValue(e) {
[b]document.getElementById('test').innerHTML = 'initial mouseover: '+e.target.value;[/b]
if (e.target.id!= 'select') {
document.getElementById('test').innerHTML = e.target.value;
}
}