Categories
JavaScript Answers

How to not allow typing alphabetic characters in a number input with JavaScript?

Sometimes, we want to not allow typing alphabetic characters in a number input with JavaScript.

In this article, we’ll look at how to not allow typing alphabetic characters in a number input with JavaScript.

How to not allow typing alphabetic characters in a number input with JavaScript?

To not allow typing alphabetic characters in a number input with JavaScript, we can listen for the keypress event and check which key is pressed when the event is triggered.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onkeypress = (evt) => {
  const charCode = evt.keyCode;
  if (charCode != 46 && charCode > 31 &&
    (charCode < 48 || charCode > 57)) {
    evt.preventDefault()
  }
}

to select an input with querySelector.

Then we add a keypress event handler on the input by setting the input.onkeypress property to a function that checks which key is pressed.

If a number key or a period isn’t pressed, then we call evt.preventDefault to stop the character for the key from being appended to the input value.

Conclusion

To not allow typing alphabetic characters in a number input with JavaScript, we can listen for the keypress event and check which key is pressed when the event is triggered.

Categories
JavaScript Answers

How to find the index of the longest array in an array of arrays with JavaScript?

Sometimes, we want to find the index of the longest array in an array of arrays with JavaScript.

In this article, we’ll look at how to find the index of the longest array in an array of arrays with JavaScript.

How to find the index of the longest array in an array of arrays with JavaScript?

To find the index of the longest array in an array of arrays with JavaScript, we can use some array methods and Math.max.

For instance, we write:

const arr = [
  [1, 2, 3, 4, 5],
  [1, 2],
  [1, 1, 1, 1, 2, 2, 2, 2, 4, 4],
  [1, 2, 3, 4, 5]
];
const lengths = arr
  .map(a => a.length)
const index = lengths
  .indexOf(Math.max(...lengths));
console.log(index)

to get the lengths of each array in arr with:

const lengths = arr
  .map(a => a.length)

Then we call lengths.indexOf to return the index of the array with the longest length.

We get the longest length with Math.max(...lengths).

We spread the lengths entries as arguments for Math.max to find the longest length.

Therefore, index is 2 since the 3rd array is the longest.

Conclusion

To find the index of the longest array in an array of arrays with JavaScript, we can use some array methods and Math.max.

Categories
JavaScript Answers

How to check if a key exists in an object with Lodash and JavaScript?

Sometimes, we want to check if a key exists in an object with Lodash and JavaScript.

In this article, we’ll look at how to check if a key exists in an object with Lodash and JavaScript.

How to check if a key exists in an object with Lodash and JavaScript?

To check if a key exists in an object with Lodash and JavaScript, we can use the has method.

For instance, we write:

const obj = {
  foo: 1,
  bar: 2
}
console.log(_.has(obj, 'foo'))
console.log(_.has(obj, 'baz'))

We call has with the obj we want to check if the key exists in and the key we’re searching for.

Therefore, the first console log logs true and the 2nd one logs false.

Conclusion

To check if a key exists in an object with Lodash and JavaScript, we can use the has method.

Categories
JavaScript Answers

How to make a promise return the resolved value instead of [object Promise] with JavaScript?

Sometimes, we want to make a promise return the resolved value instead of [object Promise] with JavaScript.

In this article, we’ll look at how to make a promise return the resolved value instead of [object Promise] with JavaScript.

How to make a promise return the resolved value instead of [object Promise] with JavaScript?

To make a promise return the resolved value instead of [object Promise] with JavaScript, we can use the await keyword.

For instance, we write:

(async () => {
  const d = await Promise.resolve(1)
  console.log(d);
})();

to call Promise.resolve with 1 to return a promise that resolves to 1.

Then we use the await keyword to get the resolved value and assign that to d.

Therefore, d is 1.

Conclusion

To make a promise return the resolved value instead of [object Promise] with JavaScript, we can use the await keyword.

Categories
JavaScript Answers

How to find an element in a documentFragment with JavaScript?

Sometimes, we want to find an element in a documentFragment with JavaScript.

In this article, we’ll look at how to find an element in a documentFragment with JavaScript.

How to find an element in a documentFragment with JavaScript?

To find an element in a documentFragment with JavaScript, we can use the querySelector method.

For instance, we write:

const df = document.createDocumentFragment();
const div = document.createElement('div');
div.id = 'foo';
df.appendChild(div);
const result = df.querySelector('#foo')
console.log(result)

to create a document fragment with createDocumentFragement.

Then we create a div with createElement.

And then we call df.appendChild with div to append the div as a child of the document fragment.

Next, we call df.querySelector with the select for the div to return the div.

Conclusion

To find an element in a documentFragment with JavaScript, we can use the querySelector method.