Remove all child elements of a DOM node in JavaScript

Option 1 A: Clearing innerHTML. This approach is simple, but might not be suitable for high-performance applications because it invokes the browser’s HTML parser (though browsers may optimize for the case where the value is an empty string). Option 1 B: Clearing textContent As above, but use .textContent. According to MDN this will be faster than innerHTML as browsers won’t invoke their HTML parsers … Read more

How to compare arrays in JavaScript?

To compare arrays, loop through them and compare every value: Comparing arrays: Usage: You may say “But it is much faster to compare strings – no loops…” well, then you should note there ARE loops. First recursive loop that converts Array to string and second, that compares two strings. So this method is faster than use … Read more

Static variables in JavaScript

If you come from a class-based, statically typed object-oriented language (like Java, C++ or C#) I assume that you are trying to create a variable or method associated to a “type” but not to an instance. An example using a “classical” approach, with constructor functions maybe could help you to catch the concepts of basic OO JavaScript: … Read more

getElementById returns null?

It can be caused by: Invalid HTML syntax (some tag is not closed or similar error) Duplicate IDs – there are two HTML DOM elements with the same ID Maybe element you are trying to get by ID is created dynamically (loaded by ajax or created by script)? Please, post your code.