Added null check required to work with IE8.

Encountered problem in IE8 without the null check.
This does not work in IE. This should be changed to: 
for (var index = 0; index < elements.length; index++)
to work in IE.