Categories
JavaScript Answers

How to get the day of the month in JavaScript?

Sometimes, we want to get the day of the month in JavaScript.

In this article, we’ll look at how to get the day of the month in JavaScript.

How to get the day of the month in JavaScript?

To get the day of the month in JavaScript, we use the getDate method.

For instance, we write

const date = new Date().getDate();

to create a new Date object with the current date and time.

Then we use getDate to get the day of the month.

Conclusion

To get the day of the month in JavaScript, we use the getDate method.

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
React Answers

How to style React Material UI text field with JavaScript?

Sometimes, we want to style React Material UI text field with JavaScript.

In this article, we’ll look at how to style React Material UI text field with JavaScript.

How to style React Material UI text field with JavaScript?

To style React Material UI text field with JavaScript, we can use the sx or inputProps prop.

For instance, we write

<TextField sx={{ marginTop: 10 }} inputProps={{ sx: { color: "#fff" } }} />;

to set the sx prop to an object with the styles for the text field.

We also set inputProps to an object with the sx property set to an object with the styles to style the text field.

Conclusion

To style React Material UI text field with JavaScript, we can use the sx or inputProps prop.

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.