Preventing Default Behavior of Forms

Preventing Default Behavior

  • Some events, such as clicking on links and submitting forms, take the user to another page or expects data to sent to a server

  • There are times when you don’t want that default behavior to happen

  • Javascript gives you the ability to prevent the default behavior using preventDefault()

  • For more information on preventDefault click here


// All javascript functions get an 'event' object as a parameter by default, most of the time
// you can ignore this, but you will need it if you want to prevent default behavior

// Define a function when 
function handleFormSubmision(event){
  event.preventDefault();

  // the rest of your code goes here

}