Can anyone explain if using the object variable selector
offers better performance compared to the traditional $("#selector")
in jQuery?
<form id="my_form" action="www.test.com" method="POST">
<input type="text" id="name">
<button type="submit" class="" style="">SEND</button>
</form>
$(document).ready(function() {
var my_form = $("#my_form"); // object variable selector
my_form.trigger("reset");
// or
$("#my_form").trigger("reset");
});
Let's just say that I will be using the $("#my_form")
more than ten times to manipulate the child elements in the DOM.
Is it better if just store the selector as an object variable for better performance or it doesn't make a difference?
my_form
after the trigger or not that the only reason to create a variable before using it. – Core972 Jun 7 at 12:29var my_form = $("#my_form");
is always preferred as long as the form does not change. That way you don't have to requery the DOM or the jquery cache. Thank you, it's a big pet peeve of mine seeing people using jquery to requery the DOM for every single change. – Shilly Jun 7 at 12:30