skip to Main Content

JQuery Optimizing Performance With Cached Element And Selector

With JQuery, it is bad practice to call a selector multiple times like this: $('#myDiv').addClass('class1'); $('#myDiv').removeClass('class2'); $('#myDiv').append(`<div>Hello World`); So it often advised to cache the selector as such: let element = $('#myDiv'); element.addClass('class1'); element.removeClass('class2'); element.append(`<div>Hello World`); But lets say for…

VIEW QUESTION
Back To Top
Search