2

I've used this bit of code to get a drop-down style menu on a website. It works absolutely fine on all browsers other than IE, and even works fine on IE except on this one page where I get the "Object doesn't support this property or method" error.

Here is where IE tells me the error is, this part is in a "header" file that is loaded first before the rest of the page.

<script type="text/javascript"> 
$(document).ready(function(){
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

if(document.all){
    $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    };
});

$.fn.hoverClass = function(c) {
    return this.each(function(){
    $(this).hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
        );
    });
};    
</script> 

I don't think the error is in the code because it works fine with no errors on every single page except for this one, this is also the only page that uses additional jQuery code. The rest of the jQuery code runs fine on the page, only the drop-down doesn't work when hovering over the menu items. If anyone can help me find the answer it would be greatly appreciated.

Thanks,

8
  • 1
    What's hoverClass? It's not part of jQuery itself. Do you need to include a certain plugin perhaps?
    – pimvdb
    Commented Nov 22, 2011 at 15:23
  • 2
    Its defined under the if statement.
    – Will
    Commented Nov 22, 2011 at 15:25
  • it is created in the script itself $.fn.hoverClass =
    – Adam
    Commented Nov 22, 2011 at 15:26
  • 1
    @Willian Van Rensselaer, that was correct u clever bastard! Stupid IE, hate it so much! please submit as a response and i will accept as correct!
    – Adam
    Commented Nov 22, 2011 at 15:27
  • 1
    You don't need a .each in hoverClass, you can just return this.hover(function(){}....
    – gen_Eric
    Commented Nov 22, 2011 at 15:29

3 Answers 3

5

When you call hoverClass, it has not yet been defined. You need to declare $.fn.hoverClass at the top of your code.

$.fn.hoverClass = function(c) {
    return this.each(function(){
    $(this).hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
        );
    });
};   
$(document).ready(function(){
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

if(document.all){
    $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    };
}); 
2

You are accessing $.fn.hoverClass before it's being declared. You should declare/load all plugins before any other code, just to be safe.

Also, in $.fn.hoverClass, you don't need .each, you can just return this.hover(). (Also, you don't need a semicolon after if statements).

$.fn.hoverClass = function(c) {
    return this.hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
    );
}; 

$(document).ready(function(){
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

    if(document.all){
        $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    }
});
1
  • 1
    +1 because your indenting was more accurate and so less confusing Commented May 13, 2013 at 22:09
0

I think the plugin is getting overridden by jQuery. May be you have included jQuery multiple times on this page. Try this

$(document).ready(function(){

$.fn.hoverClass = function(c) {
    return this.each(function(){
    $(this).hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
        );
    });
};  

    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

if(document.all){
    $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    };
});
11
  • 2
    it's simply because $.fn.hoverClass doesn't exist at the point it is called in OP...
    – Esailija
    Commented Nov 22, 2011 at 15:30
  • @Esailija - Why it doesn't exist? If the OP is adding it(check the code again). Commented Nov 22, 2011 at 15:31
  • remove the if ( document.all ) from code and try to run the code in any browser, it will fail because $.fn.hoverClass is not assigned yet. As is, it will work in other browsers because they don't have document.all and the if block won't get executed. TypeError: Object [object Object] has no method 'hoverClass' In google chrome
    – Esailija
    Commented Nov 22, 2011 at 15:34
  • The plugin is added outside $(document).ready(). As long as it is added outside it does not matter before or after. Commented Nov 22, 2011 at 15:36
  • Also OP has mentioned the same code runs in other browsers in IE. Only on this page it is giving that error. So it makes sense you revisit your comments Commented Nov 22, 2011 at 15:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.