Categories
JavaScript Answers

How to fix preventDefault not working inside passive event listener with JavaScript?

Sometimes, we want to fix preventDefault not working inside passive event listener with JavaScript.

In this article, we’ll look at how to fix preventDefault not working inside passive event listener with JavaScript.

How to fix preventDefault not working inside passive event listener with JavaScript?

To fix preventDefault not working inside passive event listener with JavaScript, we set passive to false.

For instance, we write

document.addEventListener(
  "wheel",
  (e) => {
    e.preventDefault();
    doStuff(e);
  },
  { passive: false }
);

to call addEventListener to add an event listener for the wheel event.

We set passive to false in the object in the 3rd argument so the e.preventDefault will work in the event listener.

Conclusion

To fix preventDefault not working inside passive event listener with JavaScript, we set passive to false.

Categories
JavaScript Answers

How to go back 1 folder level with __dirname with JavaScript?

Sometimes, we want to go back 1 folder level with __dirname with JavaScript.

In this article, we’ll look at how to go back 1 folder level with __dirname with JavaScript.

How to go back 1 folder level with __dirname with JavaScript?

To go back 1 folder level with __dirname with JavaScript, we can use the '../ string.

For instance, we write

const path = require("path");
const p = path.join(__dirname, "../");

to call path.join with __dirname and "../" to return a string with the path one level up from __dirname.

Conclusion

To go back 1 folder level with __dirname with JavaScript, we can use the '../ string.

Categories
JavaScript Answers

How to strip type from JavaScript FileReader base64 string?

Sometimes, we want to strip type from JavaScript FileReader base64 string.

In this article, we’ll look at how to strip type from JavaScript FileReader base64 string.

How to strip type from JavaScript FileReader base64 string?

To strip type from JavaScript FileReader base64 string, we can get the part of the base64 string after the comma.

For instance, we write

const reader: FileReader = new FileReader();

reader.onloaded = (e) => {
  const base64String = reader.result.split(",").pop();
};

to get the read file result base64 string from reader.result.

Then we split it in an array of substrings with comma as the separator with split.

Finally, we call pop to get the part of the base64 string without the type.

Conclusion

To strip type from JavaScript FileReader base64 string, we can get the part of the base64 string after the comma.

Categories
JavaScript Answers

How to run where statement with date with Sequelize?

Sometimes, we want to run where statement with date with Sequelize.

In this article, we’ll look at how to run where statement with date with Sequelize.

How to run where statement with date with Sequelize?

To run where statement with date with Sequelize, we use Op properties to form the query.

For instance, we write

const { Op } = require("sequelize");

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, "days").toDate(),
    },
  },
});

to call findAll with an object to query for entries with start_datetime bigger than or equal to 7 days ago.

Op.gte is greater than or equal to.

Conclusion

To run where statement with date with Sequelize, we use Op properties to form the query.

Categories
JavaScript Answers

How to properly unload or destroy a video element with JavaScript?

Sometimes, we want to properly unload or destroy a video element with JavaScript.

In this article, we’ll look at how to properly unload or destroy a video element with JavaScript.

How to properly unload or destroy a video element with JavaScript?

To properly unload or destroy a video element with JavaScript, we remove the src attribute from the video element.

For instance, we write

const videoElement = document.getElementById("id_of_the_video_element_here");
videoElement.pause();
videoElement.removeAttribute("src");
videoElement.load();

to get the video element with getElementById.

Then we call removeAttribute to remove the src attribute from the video element to unload it.

Conclusion

To properly unload or destroy a video element with JavaScript, we remove the src attribute from the video element.