JavaScript Does it matter when we use the preventDefault method? does it matter when you take a pregnancy test,does

Does it matter when we use the preventDefault method? (right in the beginning of a function vs at the end). All the tutorials that I have watched place preventDefault at the end of the function, but I would assume the first thing you want to do is prevent the default behavior. I have noticed that it works if it's at the beginning of the function and it works at the very end of the function:

function calculateResults(e){

    e.preventDefault();
    //UI Vars
    const amount = document.querySelector(`#amount`);
    const interest = document.querySelector(`#interest`);
    const years = document.querySelector(`#years`);
    const monthlyPayment = document.querySelector(`#monthly-payment`);
    const totalPayment = document.querySelector(`#total-payment`);
    const totalInterest =document.querySelector(`#total-interest`);

    const princapal =  parseFloat(amount.value);
    const calculatedInterest = parseFloat(interest.value)/100/12;
    const calculatedPayment = parseFloat(years.value) * 12;

    // compute monthly payments
    const x = Math.pow(1 + calculatedInterest, calculatedPayment);

}

VS

function calculateResults(e){

    //UI Vars
    const amount = document.querySelector(`#amount`);
    const interest = document.querySelector(`#interest`);
    const years = document.querySelector(`#years`);
    const monthlyPayment = document.querySelector(`#monthly-payment`);
    const totalPayment = document.querySelector(`#total-payment`);
    const totalInterest =document.querySelector(`#total-interest`);

    const princapal =  parseFloat(amount.value);
    const calculatedInterest = parseFloat(interest.value)/100/12;
    const calculatedPayment = parseFloat(years.value) * 12;

    // compute monthly payments
    const x = Math.pow(1 + calculatedInterest, calculatedPayment);

    e.preventDefault();
}

I guess my main concern is if I have a lot going on in the function is it possible that the browser will do its default behavior before it gets to preventDefault at the end? Or will it wait to do everything in the function before it attempts to do its default behavior?

Answer:1

The best practice is to call it as soon as possible.

If any error is thrown within the code before it reaches the preventDefault it may not get called and browser will perform the default action.

Once it is called however if an error is thrown after that the default action won't occur

Answer:2

There are situations where you won't always want to use e.preventDefault, which implies that where you call it within a function doesn't really matter.

Here is some example code from MDN:

function checkName(evt) {
  var charCode = evt.charCode;
  if (charCode != 0) {
    if (charCode < 97 || charCode > 122) {
      evt.preventDefault();
      displayWarning(
        "Please use lowercase letters only."
        + "\n" + "charCode: " + charCode + "\n"
      );
    }
  }
}

That said, I myself usually call it at the beginning if I absolutely intend to prevent the default action.

Answer:3

I want to Encode number to character. How Can I encode to base64 in output? Code: const CryptoJS = require('crypto-js'); function msg() { return '7543275'; // I want to encrypt this number to ...

I want to Encode number to character. How Can I encode to base64 in output? Code: const CryptoJS = require('crypto-js'); function msg() { return '7543275'; // I want to encrypt this number to ...

  1. base64 encoder cryptopp
  2. base64 encode cryptojs
  3. base64 encode using cryptojs

My passport middleware is not working. When I call passport.authenticate() on a route to confirm the user is authenticated, I am redirected to the failure page. The login route however works as ...

My passport middleware is not working. When I call passport.authenticate() on a route to confirm the user is authenticated, I am redirected to the failure page. The login route however works as ...

In node I am accustomed to handling errors with callbacks. I'm trying to make the jump to async... await, which uses a method of error handling that I am not used to. I am wondering, when throw is ...

In node I am accustomed to handling errors with callbacks. I'm trying to make the jump to async... await, which uses a method of error handling that I am not used to. I am wondering, when throw is ...

I am getting an error in IE11 when attempting to call a function that is executing on click in a radio button. Here is the html for the radio button: <input type="radio" name="filter" value="...

I am getting an error in IE11 when attempting to call a function that is executing on click in a radio button. Here is the html for the radio button: <input type="radio" name="filter" value="...

  1. javascript object doesn't support property or method
  2. javascript object doesn't support this action
  3. javascript object doesn't support includes
  4. javascript object doesn't support foreach
  5. javascript object doesn't support indexof