Fix inefficiencies in javascript and readability

Using jquery selectors multiple times to find the same elements is
inefficient and was replaced with 2 new variables.