simplified some front end code with jquery when()