Andy Coles
Andy Coles

Reputation: 126

Unable to check if element hasClass, then not animate the element

Explanation in context(WordPress): I want to check if my li element has a class called "current-menu-item" if it does I want it to stop the animate function. If it does not have that class continue animating. This script is currently not working. Thanks for any help.

$(document).ready(function ()
{
    $('.nav li a').hover(function() 
    {
        if ($(this).parentNode.hasClass('current-menu-item'))
        {
        alert('this item has the class of current-menu-item');
        }
        else
        {
        $(this).animate({color:'#3b3b3b'}, 300, 'linear');
        }
    }, 
    function() 
        {
        if ($(this).parentNode.hasClass('current-menu-item'))
        {
        // do nothing
        }
        else 
        {
            $(this).animate({color:'#999'}, 300, 'linear');
        }
    });
});

Upvotes: 1

Views: 518

Answers (1)

nnnnnn
nnnnnn

Reputation: 150080

if ($(this).parent().hasClass('current-menu-item'))

jQuery objects don't have a parentNode property. DOM elements do, but then the element returned by parentNode doesn't have jQuery methods like .hasClass(). Use jQuery's .parent() method instead.

Upvotes: 3

Related Questions