Search code examples
javascriptjqueryfunctionchildren

Know the class of a mouseenter link in a function


I have a little problem with jquery children.

I have this code :

<li><a href="#" class="gocinema">cinema</a></li>
<li><a href="#" class="gomusic">music</a></li>
<li><a href="#" class="gogame">game</a></li>

I try to set up a function for changing the color of the link on hover. (I cannot simply do that with css because, it's for a complex webdesign on a menu.)

What i try to do is to make a function who said :

What is the class of the link i am mousenter ?

If the class is that, do that. If the class is that, do that.

I have a read a lot of article on jquery children, but i don't fin how to say that in a function.

Sorry if it's not very clear :/ Thanks

Thomas


Solution

  • $(document).on('mouseenter mouseleave', 'a', function () {
    
        if ( this.className === 'gocinema' ) {
            // do this
        }
    
        if ( this.className === 'gomusic' ) {
            // do that
        }
    
    });
    

    Update

    If there's more than one class on your link, you could try $.fn.hasClass:

    $(document).on('mouseenter mouseleave', 'a', function () {
        var $this = $(this);
    
        if ( $this.hasClass('gocinema') ) {
            // do this
        }
    
        if ( $this.hasClass('gomusic') ) {
            // do that
        }
    
    });