Search code examples
javascriptjqueryswitch-statementthisonmouseover

How do you use onMouseOver with a Switch?


OnmouseOver of images #About, #Contact, or #Blog I would like to change the attribute depending on which link is mousedover using a switch statement. However, it does not work if I place the switch in a function, or try to give the switch a name. Here is what I have:

<g id="About" onmouseover="myFunction(this)"></g>
<g id="Contact" onmouseover="myFunction(this)"></g>
   <g id="Blog" onmouseover="myFunction(this)"></g>

<script type="text/javascript">


            switch myFunction (obj.id) {
    case About:
                    console.log (obj.id);
        break;
    case Contact:
                    console.log (obj.id);
        break;
    case Photos:

                    console.log (obj.id);
        break;
    case Videos:

                    console.log (obj.id);
        break;
    case Home:

             console.log (obj.id);
        break;
    case Online:

                    console.log (obj.id);
        break;
    case Blog:

                    console.log (obj.id);
}

My Inital test is as follows:

        function myFunction (obj) {

alert (obj.id);

            switch (obj.id) {
    case About:
                    console.log (obj.id);
        break;
    case Contact:
                    console.log (obj.id);
        break;
    case Photos:

                    console.log (obj.id);
        break;
    case Videos:

                    console.log (obj.id);
        break;
    case Home:

             console.log (obj.id);
        break;
    case Online:

                    console.log (obj.id);
        break;
    case Blog:

                    console.log (obj.id);
}



}

Solution

  • Your cases should be strings unless you've assigned them as variables somewhere, eg "About" rather than About.