Skip to content
Advertisement

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 you could write: You don’t need that >0

How can I beautify JavaScript code using Command Line?

I am writing a batch script in order to beautify JavaScript code. It needs to work on both Windows and Linux. How can I beautify JavaScript code using the command line tools? Answer First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at http://jsbeautifier.org/, because it’s what I found first. Downloads its file https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js Second, download and

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 variable that holds a reference to it: Obviously, if you are not using

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 just becomes: This doesn’t have to

Advertisement