I've got this piece of script here, where I'd like to slide up my ul element, then after the slide up executed, I'd like to remove the "open" class for CSS reasons. What am I doing wrong?
jQuery(this).parent().children("ul").slideUp(500);
setTimeout(function(){
var elementToRemove = jQuery(this).parent();
elementToRemove.removeClass("open");
}, 500);
thisinside you callback function refers to the global object and not the event target.Two solutions:
Use arrow functions (which preserve the
thiscontext):Or save a link to the
thisobject: