Categories
JavaScript Answers

How to get first and last day of the current week in JavaScript?

Sometimes, we want to get first and last day of the current week in JavaScript.

In this article, we’ll look at how to get first and last day of the current week in JavaScript.

How to get first and last day of the current week in JavaScript?

To get first and last day of the current week in JavaScript, we can use some date methods.

For instance, we write

const curr = new Date();
const first = curr.getDate() - curr.getDay();
const last = first + 6;

const firstDay = new Date(curr.setDate(first)).toUTCString();
const lastDay = new Date(curr.setDate(last)).toUTCString();

to create the curr date with the current datetime.

And then we get the difference between the first day of the week and the current day of the month with

curr.getDate() - curr.getDay()

And then we get the last day of the week with first + 6.

Next, we use setDate with first and last to get the first and last day of the week.

And then we create a new Date object from the dates.

We use toUTCString to return human readable date strings for each date.

Conclusion

To get first and last day of the current week in JavaScript, we can use some date methods.

Categories
JavaScript Answers

How to fix Bootstrap popover not showing on top of all elements with JavaScript?

Sometimes, we want to fix Bootstrap popover not showing on top of all elements with JavaScript.

In this article, we’ll look at how to fix Bootstrap popover not showing on top of all elements with JavaScript.

How to fix Bootstrap popover not showing on top of all elements with JavaScript?

To fix Bootstrap popover not showing on top of all elements with JavaScript, we call tooltip with an object with the container option.

For instance, we write

$("#element").tooltip({ container: "body" });

to call tooltip with { container: "body" } to set the container of the tooltip to the body element.

This will make sure that the tooltip shows on top of any element on the page.

Conclusion

To fix Bootstrap popover not showing on top of all elements with JavaScript, we call tooltip with an object with the container option.

Categories
JavaScript Answers

How to change the browser zoom level with JavaScript?

Sometimes, we want to change the browser zoom level with JavaScript.

In this article, we’ll look at how to change the browser zoom level with JavaScript.

How to change the browser zoom level with JavaScript?

To change the browser zoom level with JavaScript, we set the zoom style.

For instance, we write

document.body.style.zoom = "80%";

to set the zoom style of the body element to '80%' to set the zoom of the page to 80%.

Conclusion

To change the browser zoom level with JavaScript, we set the zoom style.

Categories
JavaScript Answers

How to make a promise from setTimeout with JavaScript?

Sometimes, we want to make a promise from setTimeout with JavaScript.

In this article, we’ll look at how to make a promise from setTimeout with JavaScript.

How to make a promise from setTimeout with JavaScript?

To make a promise from setTimeout with JavaScript, we can use the Promise constructor.

For instance, we write

const later = (delay) => {
  return new Promise((resolve) => {
    setTimeout(resolve, delay);
  });
};

to create the later function which returns a promise we create with the Promise constructor.

We create the promise by calling Promise with a callback that calls setTimeout with resolve as its callback.

The delay is in milliseconds.

The promise will resolve after delay milliseconds as a result.

Conclusion

To make a promise from setTimeout with JavaScript, we can use the Promise constructor.

Categories
JavaScript Answers

How to check whether HTML element has scrollbars with JavaScript?

Sometimes, we want to check whether HTML element has scrollbars with JavaScript.

In this article, we’ll look at how to check whether HTML element has scrollbars with JavaScript.

How to check whether HTML element has scrollbars with JavaScript?

To check whether HTML element has scrollbars with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

And we check if scrollWidth of the element is bigger than its clientWidth.

For instance, we write

const div = document.getElementById("div-id");

const hasHorizontalScrollbar = div.scrollWidth > div.clientWidth;
const hasVerticalScrollbar = div.scrollHeight > div.clientHeight;

to select the element with getElementById.

Then we check if the body element’s scrollHeight is bigger than its clientHeight to check if the body element has a vertical scrollbar.

Likewise, we check if the body element’s scrollWidth is bigger than its clientWidth to check if the body element has a horizontal scrollbar.

Conclusion

To check whether HTML element has scrollbars with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

And we check if scrollWidth of the element is bigger than its clientWidth.