Simon Rook
Simon Rook

Reputation: 205

Uncaught TypeError: Cannot read property 'removeClass' of undefined

i've got a problem with my jquery code i feel the solution is quite simple but i just can't get it, it's a simple .on click function on an element with a class But when i click on the element it gives me this error "Uncaught TypeError: Cannot read property 'removeClass' of undefined" does anyone know what i'm doing wrong here?

$("body").on( "click", ".fa-caret-right", function(e) {
    e.trigger.removeClass("fa-caret-right");             // <--- right here
    e.trigger.addClass("fa-caret-down");
    e.trigger.parent().next().css("display", "block");
});
$("body").on( "click", ".fa-caret-down", function(e) {
    e.trigger.removeClass("fa-caret-down");             // <--- and probably here
    e.trigger.addClass("fa-caret-right");
    e.trigger.parent().next().css("display", "none");
});

thanks in advance!

Upvotes: 0

Views: 16914

Answers (1)

David
David

Reputation: 218960

e is the Event object, which doesn't have a property called trigger. Since trigger doesn't exist, you can't call functions on it.

What are you trying to do here?:

e.trigger.removeClass("fa-caret-right");

If you want to remove the class from the element which invoked the handler (the element which was clicked), you can reference that element with this:

$(this).removeClass("fa-caret-right");

Or, if you want to use the Event object, you may be looking for the target property:

e.target.removeClass("fa-caret-right");

Upvotes: 2

Related Questions