Categories
JavaScript Answers

How to fix JavaScript keypress listener doesn’t detect backspace?

Sometimes, we want to fix JavaScript keypress listener doesn’t detect backspace.

In this article, we’ll look at how to fix JavaScript keypress listener doesn’t detect backspace.

How to fix JavaScript keypress listener doesn’t detect backspace?

To fix JavaScript keypress listener doesn’t detect backspace, we can listen to the keydown event instead.

For instance, we write

note.addEventListener("keydown", (event) => {
  const { key } = event;
  if (key === "Backspace") {
    // ...
  }
});

to call addEventListener on the note element with 'keydown' and the event listener function to listen for the keydown event.

In the callback, we get the key pressed with event.key.

And then we check if it’s a backspace with key === "Backspace".

Conclusion

To fix JavaScript keypress listener doesn’t detect backspace, we can listen to the keydown event instead.

Categories
JavaScript Answers

How to get the server’s port with JavaScript and Express?

Sometimes, we want to get the server’s port with JavaScript and Express.

In this article, we’ll look at how to get the server’s port with JavaScript and Express.

How to get the server’s port with JavaScript and Express?

To get the server’s port with JavaScript and Express, we can use the listener.address method.

For instance, we write

const app = require("express")();
//...
const listener = app.listen(8888, () => {
  console.log("Listening on port ", listener.address().port);
});

to call app.listen with the port to listen to.

And then we call listener.address and use the returned object’s port property to get the port being listened to.

Conclusion

To get the server’s port with JavaScript and Express, we can use the listener.address method.

Categories
JavaScript Answers

How to subtract 2 hours from user’s local time with JavaScript?

Sometimes, we want to subtract 2 hours from user’s local time with JavaScript.

In this article, we’ll look at how to subtract 2 hours from user’s local time with JavaScript.

How to subtract 2 hours from user’s local time with JavaScript?

To subtract 2 hours from user’s local time with JavaScript, we can use the setHours method.

For instance, we write

const d = new Date();
d.setHours(d.getHours() - 2);

to create the Date object d which has the user’s current local date time.

Then we call getHours to get the hour of day from date d.

And we subtract that by 2 and use the difference as the argument of setGHours to subtract 2 hours from user’s local time.

Conclusion

To subtract 2 hours from user’s local time with JavaScript, we can use the setHours method.

Categories
JavaScript Answers

How to convert a date to ISO 8601 format with time zone offset in JavaScript?

Sometimes, we want to convert a date to ISO 8601 format with time zone offset in JavaScript.

In this article, we’ll look at how to convert a date to ISO 8601 format with time zone offset in JavaScript.

How to convert a date to ISO 8601 format with time zone offset in JavaScript?

To convert a date to ISO 8601 format with time zone offset in JavaScript, we can use the toLocaleString method.

For instance, we write

const d = new Date().toLocaleString("sv", { timeZoneName: "short" });

to call toLocaleString with the locale string and { timeZoneName: "short" } to return a date time string formatted into ISO format for the given locale.

We set timeZoneName to 'short' to add the short time zone string to the date time string.

Conclusion

To convert a date to ISO 8601 format with time zone offset in JavaScript, we can use the toLocaleString method.

Categories
JavaScript Answers

How to append array to FormData and send via Ajax with JavaScript?

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator.

For instance, we write

const formData = new FormData();
const arr = ["this", "is", "an", "array"];

for (const a of arr) {
  formData.append("arr[]", a);
}

console.log(...formData);

to loop through the arr array and call formData.append with the key and value of each FormData entry.

Then we use the spread operator to spread the formData key-value entries as arguments of console.log as arrays.

Conclusion

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator.