DOM timing sequence needs to be considered for JS program to work