Categories
JavaScript Answers

How to auto close open InfoWindows with Google Maps?

Sometimes, we want to auto close open InfoWindows with Google Maps.

In this article, we’ll look at how to auto close open InfoWindows with Google Maps.

How to auto close open InfoWindows with Google Maps?

To auto close open InfoWindows with Google Maps, we call the InfoWindow’s close method.

For instance, we write

let prevInfowindow = false;
//...
base.attachInfo = (marker, i) => {
  const infoWindow = new google.maps.InfoWindow({
    content: "yourmarkerinfocontent",
  });

  google.maps.event.addListener(marker, "click", () => {
    if (prevInfowindow) {
      prevInfowindow.close();
    }

    prevInfowindow = infowindow;
    infoWindow.open(base.map, marker);
  });
};

to call prevInfowindow.close in the marker‘s click event listener to close the prevInfowindow InfoWindow.

We open the InfoWindow with infoWindow.open.

Conclusion

To auto close open InfoWindows with Google Maps, we call the InfoWindow’s close method.

Categories
JavaScript Answers

How to check if a key is down with JavaScript?

Sometimes, we want to check if a key is down with JavaScript.

In this article, we’ll look at how to check if a key is down with JavaScript.

How to check if a key is down with JavaScript?

To check if a key is down with JavaScript, we can check various properties.

For instance, we write

window.onmousemove = (e) => {
  if (e.shiftKey) {
    //...
  }
  if (e.altKey) {
    //...
  }
  if (e.ctrlKey) {
    //...
  }
  if (e.metaKey) {
    //...
  }
};

to get the key pressed in the window.onmousemove method, which runs when we move the mouse.

We check if the shift key is pressed with e.shiftKey.

We check if the altkey is pressed with e.altKey.

We check if the ctrl key is pressed with e.ctrlKey.

And we check if the Windows or command key is pressed with e.metaKey.

Categories
JavaScript Answers

How to start HTML5 video at a particular position when loading with JavaScript?

Sometimes, we want to start HTML5 video at a particular position when loading with JavaScript.

In this article, we’ll look at how to start HTML5 video at a particular position when loading with JavaScript.

How to start HTML5 video at a particular position when loading with JavaScript?

To start HTML5 video at a particular position when loading with JavaScript, we can set the currentTime property to the number of seconds we want to jump to.

For instance, we write

document.getElementById("vid1").addEventListener(
  "loadedmetadata",
  (e) => {
    e.target.currentTime = 50;
  },
  false
);

to select the video element with getElementById.

Then we call addEventListener to listen to the loadedmetadata event, which is triggered when the video metadata is loaded.

In the event listener callback, we set the currentTime property of the video element to jump to 50 seconds.

Conclusion

To start HTML5 video at a particular position when loading with JavaScript, we can set the currentTime property to the number of seconds we want to jump to.

Categories
JavaScript Answers

How to generate a random number of fixed length using JavaScript?

Sometimes, we want to generate a random number of fixed length using JavaScript.

In this article, we’ll look at how to generate a random number of fixed length using JavaScript.

How to generate a random number of fixed length using JavaScript?

To generate a random number of fixed length using JavaScript, we can use the Math.floor and Math.random methods.

For instance, we write

console.log(Math.floor(100000 + Math.random() * 900000));

to generate a 6 digit number with

100000 + Math.random() * 900000

Then we get rid of the decimals by returning the floor of the number with Math.floor.

Conclusion

To generate a random number of fixed length using JavaScript, we can use the Math.floor and Math.random methods.

Categories
JavaScript Answers

How to get the position of a div or span tag with JavaScript?

Sometimes, we want to get the position of a div or span tag with JavaScript.

In this article, we’ll look at how to get the position of a div or span tag with JavaScript.

How to get the position of a div or span tag with JavaScript?

To get the position of a div or span tag with JavaScript, we can use the getBoundingClientRect method.

For instance, we write

const offsets = document.getElementById("foo").getBoundingClientRect();
const { top, left } = offsets;

to call getElementById to select the element we want to get the position for.

Then we call getBoundingClientRect on it to get an object with the position values.

And then we can get the top and left position offset with the top and left properties.

Conclusion

To get the position of a div or span tag with JavaScript, we can use the getBoundingClientRect method.