Javascript DOM query is outside of the listener