There are some times when I find myself repeating a selector several times. Should I be somehow storing a jquery object to a variable and then just using that one? As a quick example, what about the following?:
$('a.contactus').css('padding', '10px');
$('a.contactus').css('margin', '4px');
$('a.contactus').css('display', 'block');
Now I know this isn’t a great example, since effectively you could just chain each css function. But suppose in between each of those was a conditional statement or something to stop you from chaining.
Can I store a jquery object in a variable? And if so, when should I / can I?
You can do this
but for readability i do this
basically every time you use $(someselector) you iterate through the dom. If you can you should store the element reference.