addClass () is a jQuery method, but this is a direct reference to a DOM object. Before using the jQuery method, you need to wrap this in a jQuery object.
$(document).ready(function(){ $(".navlink").click(function(){ $(".navlink").removeClass("active"); $(this).addClass("active"); }); });
DEMO - using $(this).addClass() instead of this.addClass()
Edit
Think a little about it. You can never call the jQuery addClass() method on a JavaScript object, because addClass() is a jQuery method.
To do the same in pure JavaScript, if you just want to use this , you can use element.className , similar to this:
$(document).ready(function(){ $(".navlink").click(function(){ $(".navlink").removeClass("active");
DEMO - Using this.className example
Although, if you are using jQuery already, it makes no sense not to use $(this).addClass() instead.
source share