Skip to content

Is there an “exists” function for jQuery?

How can I check the existence of an element in jQuery? The current code that I have is this: Is there a more elegant way to approach this? Perhaps a plugin or a function? Answer In JavaScript, everything is ‘truthy’ or ‘falsy’, and for numbers 0 means false, everything else true. So yo…

How can I remove a child node in HTML using JavaScript?

Is there a function like document.getElementById(“FirstDiv”).clear()? Answer To answer the original question – there are various ways to do this, but the following would be the simplest. If you already have a handle to the child node that you want to remove, i.e. you have a JavaScript variab…

Length of a JavaScript object

I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? Answer Updated answer Here’s an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use Object.keys() so the above code …