Is it possible to change the function called by the existing onmouseover or onmouseout event?
Yes, by writing to the DOM property element.onmouseover with the value of the function. It can be a function name or an inline function expression.
If you run all of your scripts by writing down the properties of an event handler (or adding event listeners), you can use this to point to an element and avoid going around around identifiers, which simplifies it:
<span id="ItemA"> <button type="button" id="ButtonB"> <script type="text/javascript"> function ChangeColor() { this.style.background= 'orange'; this.style.color= 'black'; } function ChangeColorBack(elementid) { this.style.background= 'black'; this.style.color= 'white'; } document.getElementById('ItemA').onmouseover= ChangeColor; document.getElementById('ButtonB').onclick= function() { document.getElementById('ItemA').onmouseover= ChangeColorBack; }; </script>
However, for this type of work with hovering and selecting, you are usually better off using state variables or CSS instead of re-assigning event handlers. For example, something like:
#ItemA:hover { background: orange; color: black; }
( :hover on the run does not work in IE6, so if you need to highlight this browser, you need to have onmouseover / mouseout to add or remove .hover className.)
bobince
source share