Atma
Atma

Reputation: 29767

Error when trying to add class to parent in jQuery

I have trying to add a class to parent div of my link like this:

$(document).ready(function() {
        $(".food-result").on("click", "a", function (event) {
            event.preventDefault(); 
            $(this).parent().parent().addclass('active');


            return false; 
        });

I get the error:

Uncaught TypeError: undefined is not a function 

What am I doing wrong?

Upvotes: 0

Views: 86

Answers (3)

Warrenn enslin
Warrenn enslin

Reputation: 1036

it looks like you are trying to get the parent of the parent which might not exist would this work

$(document).ready(function() {
    $(".food-result").on("click", "a", function (event) {
        event.preventDefault(); 
        $(this).parent().addClass('active');


        return false; 
    });

Upvotes: 0

Cornel Raiu
Cornel Raiu

Reputation: 2995

try this:

jQuery(document).ready(function($) {
    $(".food-result").click(function (event) {
        event.preventDefault(); 
        $(this).parent().parent().addClass('active');
        return false; // I am not sure if this is needed
    });
});

Upvotes: 0

Max
Max

Reputation: 1844

Typo in addclass method name. Try addClass. More details

Upvotes: 3

Related Questions