Do not rely on DOM specific properties.
Simplified the nested if/else.