Categories
JavaScript Answers

How to check if date is a valid one with JavaScript?

Sometimes, we want to check if date is a valid one with JavaScript.

In this article, we’ll look at how to check if date is a valid one with JavaScript.

How to check if date is a valid one with JavaScript?

To check if date is a valid one with JavaScript, we can use the moment.js’ isValid function.

For instance, we write

const date = moment("2022-10-19").isValid();

to call moment with a date string.

Then we call isValid to check if the date string is a valid one.

Conclusion

To check if date is a valid one with JavaScript, we can use the moment.js’ isValid function.

Categories
JavaScript Answers

How to get the browser’s scrollbar sizes with JavaScript?

Sometimes, we want to get the browser’s scrollbar sizes with JavaScript.

In this article, we’ll look at how to get the browser’s scrollbar sizes with JavaScript.

How to get the browser’s scrollbar sizes with JavaScript?

To get the browser’s scrollbar sizes with JavaScript, we can subtract the window’s width by the html element’s width.

For instance, we write

const scrollBarWidth =
  window.innerWidth - document.getElementsByTagName("html")[0].clientWidth;

to get the window’s width with window.innerWidth.

And then we subtract that by the html element’s width which we get by

document.getElementsByTagName("html")[0].clientWidth

to get the scrollbar’s width.

Conclusion

To get the browser’s scrollbar sizes with JavaScript, we can subtract the window’s width by the html element’s width.

Categories
JavaScript Answers

How to change source on HTML5 video tag with JavaScript?

Sometimes, we want to change source on HTML5 video tag with JavaScript.

In this article, we’ll look at how to change source on HTML5 video tag with JavaScript.

How to change source on HTML5 video tag with JavaScript?

To change source on HTML5 video tag with JavaScript, we an set the src property of the video element.

For instance, we write

const changeSource = (url) => {
  const video = document.getElementById("video");
  video.src = url;
  video.play();
};

to define the changeSource function.

In it, we get the video with getElementById.

And then we set the src property of the video element to the url to change the video source.

Conclusion

To change source on HTML5 video tag with JavaScript, we an set the src property of the video element.

Categories
JavaScript Answers

How to remove objects from array by object property with JavaScript?

Sometimes, we want to remove objects from array by object property with JavaScript.

In this article, we’ll look at how to remove objects from array by object property with JavaScript.

How to remove objects from array by object property with JavaScript?

To remove objects from array by object property with JavaScript, we call array map with to get the values of the property in each object.

And then we get the index of it with indexOf and call splice to remove the item at the given index in the original array.

For instance, we write

const removeIndex = array.map((item) => item.id).indexOf("abc");
array.splice(removeIndex, 1);

to call array.map to get the value of the id property in each item in array.

Then we get the index of the first item with id 'abc' with indexOf.

Next, we call array.splice with the removeIndex and 1 to remove the item in array at removeIndex in place.

Conclusion

To remove objects from array by object property with JavaScript, we call array map with to get the values of the property in each object.

And then we get the index of it with indexOf and call splice to remove the item at the given index in the original array.

Categories
JavaScript Answers

How to fix scrollIntoView() causing the whole page to move with JavaScript?

Sometimes, we want to fix scrollIntoView() causing the whole page to move with JavaScript.

In this article, we’ll look at how to fix scrollIntoView() causing the whole page to move with JavaScript.

How to fix scrollIntoView() causing the whole page to move with JavaScript?

To fix scrollIntoView() causing the whole page to move with JavaScript, we can call scrollIntoView with a few options.

For instance, we write

element.scrollIntoView({
  behavior: "smooth",
  block: "nearest",
  inline: "start",
});

to call scrollIntoView with an object that jumps to the nearest block element with block set to 'nearest'.

And we jump to the start of the inline element with inline set to 'start'.

Conclusion

To fix scrollIntoView() causing the whole page to move with JavaScript, we can call scrollIntoView with a few options.