improved js code to prevent repetitive dom refs