Search code examples
jqueryinternet-explorer-7jquery-hover

Why does this simple JQuery Hover Effect work everywhere EXCEPT in IE7 thru IE9?


Being new to jQuery (but not vanilla JS) I am completely baffled why the following hover effect works in FF, Chrome, Safari, but not in IE! I got this script from one of Carl Meyer's post here; changing only the object id's to match my markup.

You can find a working example of this page here, but here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
<title>test</title>  
    <script type="text/javascript" language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript" language="JavaScript">
        $(document).ready(function() {
            toolButtons = $('#toolButtons > li');
            insideCenter = $('#insideCenter > div');
            toolButtons.each(function(idx) {
                    $(this).data('slide', insideCenter.eq(idx));
                }).hover(
                function() {
                    toolButtons.removeClass('active');
                    insideCenter.removeClass('active');             
                    $(this).addClass('active');  
                    $(this).data('slide').addClass('active');
                });
            });
    </script>
    <style type="text/css">  
        #toolButtons .active {   font-weight: bold; }  
        #insideCenter div {   display: none; }  
        #insideCenter div.active {   display: block; }  
    </style> 
 </head>

 <body>
    <ul id="toolButtons">   
        <li class="active">First slide</li>   
        <li>Second slide</li>   
        <li>Third slide</li>   
        <li>Fourth slide</li> 
    </ul> 
    <div id="insideCenter">   
        <div id="slide1" class="active">Well well.</div>   
        <div id="slide2">Oh no!</div>   
        <div id="slide3">You again?</div>   
        <div id="slide4">I'm gone!</div> 
    </div>
 </body>
</html>

Any help is greatly appreciated!

vinnie


Solution

  • Call var toolButtons = $('#toolButtons > li'); and var insideCenter = $('#insideCenter > div');.

    You dropped the var and it can't be seen as it is trying to set a global variable. Use var whenever possible, especially if the variable is not global.

    EDIT: Pure jQuery:

    $(document).ready(function() {             
       $('#toolButtons > li').each(function(idx) {                     
            $(this).data('slide', $('#insideCenter > div').eq(idx));                 
        }).hover(                 
            function() {                     
                $('#toolButtons > li').removeClass('active');
                $('#insideCenter > div').removeClass('active');                                  
                $(this).addClass('active');                       
                $(this).data('slide').addClass('active');                 
            });             
    });