Categories
JavaScript Answers

How to hide a div when it loses focus with JavaScript?

Sometimes, we want to hide a div when it loses focus with JavaScript.

In this article, we’ll look at how to hide a div when it loses focus with JavaScript.

How to hide a div when it loses focus with JavaScript?

To hide a div when it loses focus with JavaScript, we can listen to the mouseup event on the document.

And if we lift our mouse button anywhere outside the div, then we hide the div.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
document.onmouseup = (e) => {
  if (e.target !== div) {
    div.style.display = 'none'
  }
}

to select a div.

Then we set document.onmouseup to a function that checks if the element being clicked isn’t a div with e.target !== div.

e.target is the element where the event is being triggered.

If it’s true, then we set the display CSS property of the div to 'none' to hide it.

Conclusion

To hide a div when it loses focus with JavaScript, we can listen to the mouseup event on the document.

And if we lift our mouse button anywhere outside the div, then we hide the div.

Categories
JavaScript Answers

How to get element By using class name with JavaScript?

Sometimes, we want to get element By using class name with JavaScript.

In this article, we’ll look at how to get element By using class name with JavaScript.

How to get element By using class name with JavaScript?

To get element By using class name with JavaScript, we can use the getElementsByClassName method.

For instance, we write:

<div class='foo'>

</div>
<div class='foo'>

</div>
<div class='foo'>

</div>

to add 3 divs with class foo.

Then we write:

const els = document.getElementsByClassName('foo')
console.log(els)

to call getElementsByClassName with 'foo' to return all the elements with class name foo in an HTMLCollection object.

Conclusion

To get element By using class name with JavaScript, we can use the getElementsByClassName method.

Categories
JavaScript Answers

How to set date always to eastern time regardless of user’s time zone with JavaScript?

To set date always to eastern time regardless of user’s time zone with JavaScript, we can use the moment-timezone library.

To use it, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone.min.js" ></script>

to add the moment.js and moment-timezone scripts.

Then we write:

moment.tz.add([
  'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);

const d = new Date(2022, 5, 1);
const myTimezone = "America/New_York";
const myDatetimeFormat = "YYYY-MM-DD hh:mm:ss a z";
const myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);
console.log(myDatetimeString)

to call moment.tz.add to add the time zone data for Eastern time.

Then we create a date d with the Date constructor.

Next, we call moment with d to create a moment object.

Then we call tz with myTimezone to convert the time to the given time zone.

Next, we call format to return a date string with the format specified by myDatetimeFormat.

Therefore, myDatetimeString is '2022-06-01 03:00:00 am EDT'.

Categories
JavaScript Answers

How to prompt user before browser close with JavaScript?

Sometimes, we want to prompt user before browser close with JavaScript.

In this article, we’ll look at how to prompt user before browser close with JavaScript.

How to prompt user before browser close with JavaScript?

To prompt user before browser close with JavaScript, we can return true in the beforeunload event handler.

For instance, we write:

window.onbeforeunload = (evt) => {
  return true;
}

to set the window.onbeforeunload property to a function that returns true to listen to the beforeunload event.

The beforeunload event is triggered right before we unload the page.

And since it returns true, we see a prompt before we close the browser tab.

Conclusion

To prompt user before browser close with JavaScript, we can return true in the beforeunload event handler.

Categories
JavaScript Answers

How to convert HSB/HSV color to HSL with JavaScript?

Sometimes, we want to convert HSB/HSV color to HSL with JavaScript.

In this article, we’ll look at how to convert HSB/HSV color to HSL with JavaScript.

How to convert HSB/HSV color to HSL with JavaScript?

To convert HSB/HSV color to HSL with JavaScript, we can use the formula in this wiki page.

For instance, we write:

const hsl2hsv = (h, s, l, v = s * Math.min(l, 1 - l) + l) => [h, v ? 2 - 2 * l / v : 0, v];

const hsv2hsl = (h, s, v, l = v - v * s / 2, m = Math.min(l, 1 - l)) => [h, m ? (v - l) / m : 0, l];

console.log(hsl2hsv(1, 2, 3, 4))
console.log(hsv2hsl(1, 2, 3, 4, 5))

to define the hsl2hsv and hsv2hsl functions to do the calculations.

In each function, we return the calculated values in an array.

Therefore, the first console log logs [1, 0.5, 4].

And the first console log logs [1, -0.2, 4].

Conclusion

To convert HSB/HSV color to HSL with JavaScript, we can use the formula in this wiki page.