I recently had a question about how to correctly check if an element exists with jQuery. I found the answer from here:
https://learn.jquery.com/using-jquery-core/faq/how-do-i-test-whether-an-element-exists/
In summary:
if ( $( #myDiv ).length ) {
// Do something
}
One guy I work with says the correct way to check should be:
if ($( #myDiv ) && $( #myDiv ).length ) {
// Do something
}
Does it matter? I mean from a performance or latency wise, do they perform the same?
Also:
$( #myDiv ).show();
$( #myDiv ).hide();
$( #myDiv ).val('');
In these type of jQuery functions, it seems no if
check is needed because they won't raise an error if #myDiv
does not exist, correct?
For what it's worth, I am using jQuery 1.6.4.