Categories
JavaScript Answers

How to convert enums to array of values with JavaScript?

Sometimes, we want to convert enums to array of values with JavaScript.

In this article, we’ll look at how to convert enums to array of values with JavaScript.

How to convert enums to array of values with JavaScript?

To convert enums to array of values with JavaScript, we can use the Object.values method.

For instance, we write:

const types = {
  "WHITE": 0,
  "BLACK": 1
}
const vals = Object.values(types)
console.log(vals)

to call Object.values with types to return an array of the property values.

Therefore, vals is [0, 1].

Conclusion

To convert enums to array of values with JavaScript, we can use the Object.values method.

Categories
JavaScript Answers

How to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour?

Sometimes, we want to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour.

In this article, we’ll look at how to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour.

How to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour?

To change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour, we can use the % operator.

For instance, we write:

const hours12 = (date) => {
  return (date.getHours() + 24) % 12 || 12;
}

const d = new Date('December 25, 2022 23:15:30')
console.log(hours12(d))

We define the hours12 to return the hours of the date in 12 hour format instead of 24.

In the function, we call date.getHours to get the hours, which is between 1 and 24.

Then we convert it to 12 hour format by adding 24 and then use the % to get the remainder when the result is divided by 12.

If the remainder is 0, then we return 12.

Therefore, the console log should log 11.

Conclusion

To change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour, we can use the % operator.

Categories
JavaScript Answers

How to format datetime in JavaScript?

Sometimes, we want to format datetime in JavaScript.

In this article, we’ll look at how to format datetime in JavaScript.

How to format datetime in JavaScript?

To format datetime in JavaScript, we can use the moment.js’ format method.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

to add the moment script.

Then we write:

const s = moment("2022-01-17T08:44:29+0100").format('MM/DD/YYYY');
console.log(s)

to call moment with a datetime string to create a moment object.

Then we call format on it with a format string to format it in the format specified.

Therefore, s is '01/16/2022'.

Conclusion

To format datetime in JavaScript, we can use the moment.js’ format method.

Categories
JavaScript Answers

How to prevent middle mouse click scrolling with JavaScript?

Sometimes, we want to prevent middle mouse click scrolling with JavaScript.

In this article, we’ll look at how to prevent middle mouse click scrolling with JavaScript.

How to prevent middle mouse click scrolling with JavaScript?

To prevent middle mouse click scrolling with JavaScript, we can listen to the mousedown event on the body element.

Then we check if the middle mouse button is clicked and prevent the default scrolling behavior if it is.

For instance, we write:

document.body.onmousedown = (e) => {
  if (e.button === 1) {
    e.preventDefault();
    return false;
  }
}

We set document.body.onmousedown to a function that checks if the middle mouse button is clicked.

We do that checking if e.button is 1.

If it is, then we call e.preventDefault and return false to prevent the default scrolling behavior.

Conclusion

To prevent middle mouse click scrolling with JavaScript, we can listen to the mousedown event on the body element.

Then we check if the middle mouse button is clicked and prevent the default scrolling behavior if it is.

Categories
JavaScript Answers

How to make a HTML5 datalist visible when focus event fires on input with JavaScript?

Sometimes, we want to make a HTML5 datalist visible when focus event fires on input with JavaScript.

In this article, we’ll look at how to make a HTML5 datalist visible when focus event fires on input with JavaScript.

How to make a HTML5 datalist visible when focus event fires on input with JavaScript?

To make a HTML5 datalist visible when focus event fires on input with JavaScript, we can save the old input value on mouse move. On mouse down, we can empty the input value. And on mouse up, we set the input value to the saved input value.

For instance, we write:

<input name="input" list="input" />

<datalist id="input">
  <option value="apple" selected></option>
  <option value="orange"></option>
  <option value="grape"></option>
</datalist>

to add an input and datalist associated with the input.

Then we write:

const input = document.querySelector('input')
let old;
input.onmousemove = (e) => {
  e.target.focus()
  old = e.target.value
}

input.onmousedown = (e) => {
  e.target.value = ''
}

input.onmouseup = (e) => {
  e.target.value = old
}

to select the input and define the old variable to store the old value.

The we focus on the input and save the old value on mouse move with:

input.onmousemove = (e) => {
  e.target.focus()
  old = e.target.value
}

Then on mouse down, we empty the input value with:

input.onmousedown = (e) => {
  e.target.value = ''
}

Then on mouse up, we set the input value to the saved old value with:

input.onmouseup = (e) => {
  e.target.value = old
}

Conclusion

To make a HTML5 datalist visible when focus event fires on input with JavaScript, we can save the old input value on mouse move. On mouse down, we can empty the input value. And on mouse up, we set the input value to the saved input value.