Categories
JavaScript Answers

How to prevent select on input text field with JavaScript?

Sometimes, we want to prevent select on input text field with JavaScript.

In this article, we’ll look at how to prevent select on input text field with JavaScript.

How to prevent select on input text field with JavaScript?

To prevent select on input text field with JavaScript, we can set the input’s selectionStart property to the value of the input’s selectionEnd property.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input');
input.onselect = (e) => {
  e.target.selectionStart = e.target.selectionEnd;
}

to select the input with querySelector.

Then set the input.onselect property to a function that sets e.target.selectionStart to e.target.selectionEnd to set the selection range to length 0 to prevent selection.

Conclusion

To prevent select on input text field with JavaScript, we can set the input’s selectionStart property to the value of the input’s selectionEnd property.

Categories
JavaScript Answers

How to limit array size with JavaScript?

Sometimes, we want to limit array size with JavaScript.

In this article, we’ll look at how to limit array size with JavaScript.

How to limit array size with JavaScript?

To limit array size with JavaScript, we can use the array slice method.

For instance, we write:

const arr = [1, 2, 3]
const add = (a, x) => [x, ...a.slice(0, a.length - 1)];
console.log(add(arr, 4))

to define the add function that takes an array a and value x that we prepend to the returned array.

We keep the returned array the same size as a by calling slice with 0 and a.length - 1 to discard the last item in a.

Therefore, the console log logs [4, 1, 2].

Conclusion

To limit array size with JavaScript, we can use the array slice method.

Categories
JavaScript Answers

How to make a checkbox without a form with HTML and JavaScript?

Sometimes, we want to make a checkbox without a form with HTML and JavaScript.

In this article, we’ll look at how to make a checkbox without a form with HTML and JavaScript.

How to make a checkbox without a form with HTML and JavaScript?

To make a checkbox without a form with HTML and JavaScript, we can add an input element with type checkbox.

Then we can get its value when we check or uncheck it by attaching a change event handler to it.

For instance, we write:

<input type="checkbox" />

to add an input with the type attribute set to checkbox to add a checkbox.

Then we write:

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

to select the input with querySelector.

Then we set the input.onchange property to a function that gets the checked value of the checkbox from e.target.checked.

Conclusion

To make a checkbox without a form with HTML and JavaScript, we can add an input element with type checkbox.

Then we can get its value when we check or uncheck it by attaching a change event handler to it.

Categories
JavaScript Answers

How to convert local time to UTC time with moment.js and JavaScript?

Sometimes, we want to convert local time to UTC time with moment.js and JavaScript.

In this article, we’ll look at how to convert local time to UTC time with moment.js and JavaScript.

How to convert local time to UTC time with moment.js and JavaScript?

To convert local time to UTC time with moment.js and JavaScript, we can use the moment utc and format method.

For instance, we write:

const d = new Date(2022, 1, 1)
const s = moment.utc(d).format("YYYY-MM-DD HH:mm:ss Z")
console.log(s)

to create the date d in local time.

Then we create a UTC moment object from d with moment.utc.

Next, we call format to with a format string that returns the date and time with the time zone.

As a result, s is '2022-02-01 08:00:00 +00:00'.

Conclusion

To convert local time to UTC time with moment.js and JavaScript, we can use the moment utc and format method.

Categories
JavaScript Answers

How to create a UTC JavaScript date?

Sometimes, we want to create a UTC JavaScript date.

In this article, we’ll look at how to create a UTC JavaScript date.

How to create a UTC JavaScript date?

To create a UTC JavaScript date, we can use the Date.UTC method.

For instance, we write:

const d = new Date(Date.UTC(2022, 10, 5));
console.log(d.toUTCString());

to call Date.UTC to create a timestamp set to Nov 5, 2022 in UTC.

Then we convert that to a JavaScript date object with the Date constructor.

Therefore, d.toUTCString returns 'Sat, 05 Nov 2022 00:00:00 GMT'.

Conclusion

To create a UTC JavaScript date, we can use the Date.UTC method.