moved event.preventDefault inside if-then structure