Fixed bug caused by referencing unused elements

Also adding in a cross-browser event handler