Check 'getElementById' ID Exists First?

In my page templates, which are included on every page, I have some javascript that is necessary to create some effects. However a couple of pages do not require the javascript, and because of this the page elements are not present. How would I add a check to ensure the element ID exists before running the javascript code? In other words, here’s the code in question…

var thevar = document.getElementById("theid").getElementsByTagName("LI");

Is there a conditional that can be wrapped around that to prevent the javascript code from running if no elements named “theid” exist?

You could always try the obvious:


var ele = document.getElementById("theid");

if (ele) {
   var theVar = ele.getElementsByTagName("LI");
   ...
}

Duh. It’s too early on a Saturday to be coding :wink: Thanks!