Whenever I want to find if an element exist in the DOM I use the following code.
if($('#target').length){
// do stuff.
}
This works well and I use it quite a lot on client sites.
Question:
How fast is this method? What are the speed implications when this is used a lot in a project?
You would be much better off using
if(document.getElementById('target'))instead. JavaScript is always faster than jQuery (since jQuery is just a bunch of JavaScript hidden under the carpet)EDIT: If you use it a lot, you can make a custom function: