Categories
JavaScript Answers

How to pass multiple arguments into a JavaScript callback function?

Sometimes, we want to pass multiple arguments into a JavaScript callback function.

In this article, we’ll look at how to pass multiple arguments into a JavaScript callback function.

How to pass multiple arguments into a JavaScript callback function?

To pass multiple arguments into a JavaScript callback function, we can use the spread operator.

For instance, we write:

const doSomething = (...args) => {
  console.log(args)
}

class SomeClass {
  constructor(callback, params) {
    this.callback = callback;
    this.params = params
  }

  method() {
    this.callback(...this.params)
  }
}

const a = ['v1text', 'v2text']
const obj = new SomeClass(doSomething, a);
obj.method()

to create the someClass class that takes a callback function and params parameters.

We set them both as values for properties of this.

And we define the method method that calls this.callback with this.params spread as arguments.

Next, we create a SomeClass instance with

const obj = new SomeClass(doSomething, a);

And then we call obj.method to call doSomething with a array.

Therefore, we see ["v1text", "v2text"] logged from doSomething.

Conclusion

To pass multiple arguments into a JavaScript callback function, we can use the spread operator.

Categories
JavaScript Answers

How to catch the moment the input box lost focus with JavaScript?

Sometimes, we want to catch the moment the input box lost focus with JavaScript.

In this article, we’ll look at how to catch the moment the input box lost focus with JavaScript.

How to catch the moment the input box lost focus with JavaScript?

To catch the moment the input box lost focus with JavaScript, we can listen for the blur event.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onblur = () => {
  console.log('blurred')
}

to select the input with querySelector.

And then we set input.onblur to a function that logs 'blurred'.

Therefore, when we move focus away from the input, we see 'blurred' logged.

Conclusion

To catch the moment the input box lost focus with JavaScript, we can listen for the blur event.

Categories
JavaScript Answers

How to get the change event for a datalist with JavaScript?

Sometimes, we want to get the change event for a datalist with JavaScript.

In this article, we’ll look at how to get the change event for a datalist with JavaScript.

How to get the change event for a datalist with JavaScript?

To get the change event for a datalist with JavaScript, we can listen to the input’s change event.

For instance, we write:

<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />

<datalist id="ice-cream-flavors">
  <option value="Chocolate">
  <option value="Coconut">
  <option value="Mint">
  <option value="Strawberry">
  <option value="Vanilla">
</datalist>

to add the input with a datalist with the options.

Then we write:

const input = document.querySelector('input')
input.onchange = (e) => {
  console.log(e.target.value)
}

to select the input with querySelector.

And then we set input.onchange to a function that logs the selected value.

As a result, when we select an option, we see the value attribute of the option element selected logged.

Conclusion

To get the change event for a datalist with JavaScript, we can listen to the input’s change event.

Categories
JavaScript Answers

How to add the vue.config.js file into a Vue CLI Vue project?

Sometimes, we want to add the vue.config.js file into a Vue CLI Vue project.

In this article, we’ll look at how to add the vue.config.js file into a Vue CLI Vue project.

How to add the vue.config.js file into a Vue CLI Vue project?

To add the vue.config.js file into a Vue CLI Vue project, we can add it to the root of the project folder.

In it, we write:

module.exports = {
  // options...
}

And we add all the options within the exported object.

Conclusion

To add the vue.config.js file into a Vue CLI Vue project, we can add it to the root of the project folder.

Categories
JavaScript Answers

How to cancel keydown with JavaScript?

Sometimes, we want to cancel keydown with JavaScript.

In this article, we’ll look at how to cancel keydown with JavaScript.

How to cancel keydown with JavaScript?

To cancel keydown with JavaScript, we can call preventDefault in the keydown event handler.

For instance, we write:

document.onkeydown = (evt) => {
  const cancelKeypress = /^(13|32|37|38|39|40)$/.test(evt.keyCode.toString());
  if (cancelKeypress) {
    evt.preventDefault()
  }
};

to set document.onkeydown to a function that checks if the evt.keyCode matches any one of the numbers in the regex.

evt.keyCode is the code of the key that’s pressed.

If there’s a match, then we call evt.preventDefault to stop the default action.

Conclusion

To cancel keydown with JavaScript, we can call preventDefault in the keydown event handler.