JQuery allows us to directly use the $ .css () method and pass the original DOM element as the first parameter. For example, if I want to set 'a' to the width of myDiv, jQuery allows this syntax:
(OPTION 1):
var elem = document.getElementById('myDiv'); var a = $.css(elem, 'width');
instead (OPTION 2):
var a = $('#myDiv').css('width');
Option 1 does not require a selector, and it seems to rely on a global jQuery object, rather than creating a new one. I can not find documentation in jQuery API or online about this method. I suppose this will be an increase in performance, especially when jQuery objects are required in the animation. Any reason I should not use this method?
Thanks!
EDIT: Perf tests show that option 1 is a bit faster. It doesn't seem like there is any reason not to use $ .css () directly. Thanks everyone for the answers!
source share