Is it faster to first check if an element exists, and then bind event handlers, like this:
if( $(\'.selector\').length ) {
$(\'.selector\').on(\'click\',
Use the binding directly.
$('selector').on('click',function() {
// Do stuff on click
}
If if there are no elements with the specified selector, it won't do anything - not even post an error, which means that actual check for the element would be doing the same thing twice. I wouldn't call it exactly a check of the existence of an element, but the way this is implemented in jQuery certainly acts that way.