Hello Readers,
In jquery when we use the two or more than two same id's as a selector or elements and operate over jquery event than sometimes effect on its performance due to caching.
For Example :
$("#mydemo").css("color", "red");
//Doing some other stuff
$("#mydemo").text("Error occurred");
Now in above jQuery code id #mydemo is used two times. So, both the times jQuery traverse down the DOM tree with the same id #mydemo . So, if you overcome this caching we use to store the selector and its event in a variable and attach this varaible into another event.
For Example :
var $myElement = $("#mydemo").css("color", "red");
//Doing some other stuff
$myElement.text("Error occurred!");
So,in the above code, jQuery use the id #mydemo with CSS event and store into the myElement variable after that use this varible when we use second event in jquery instead of calling again the same id in jquery.
0 Comment(s)