jQuery Element exists function

Hello, So  a friend of mine asked me a question that if there is a function in jQuery that makes it simple to check if a element with an ID exist in DOM or not. So I think that function does not exist but its pretty simple to write one. Ok first things first, here is a simple way to check if an element exist or not. Markup Here is an example markup on which our jQuery code will be based on [crayon-5f0e1a78b97da169230680/]  A simple check (This is how you do it anyway) [crayon-5f0e1a78b97e2474526270/] So above works pretty well and it will work pretty well with the CSS class selectors too. But just if you are after exists() function here is what you can do :) The only advantage is readability of your code. Now lets write a little plugin so that you can hook it in your code easily [crayon-5f0e1a78b97e4344361374/]     So to be generic in nature here is a generic definition for our exists() function for jQuery [crayon-5f0e1a78b97e5369491759/] [crayon-5f0e1a78b97e6000158446/] To make our function return boolean true or false we just need to change the condition in our plugin function as shown below [crayon-5f0e1a78b97e7415461765/] I hope that this helps. If you would like to add something or make a correction please leave your comments Cheers     … [Read more...]