Categories
JavaScript Answers

How to detect click inside/outside of element with single event handler with JavaScript?

Sometimes, we want to detect click inside/outside of element with single event handler with JavaScript.

In this article, we’ll look at how to detect click inside/outside of element with single event handler with JavaScript.

How to detect click inside/outside of element with single event handler with JavaScript?

To detect click inside/outside of element with single event handler with JavaScript, we use the element contains method.

For instance, we write

const myElementToCheckIfClicksAreInsideOf =
  document.querySelector("#my-element");

document.body.addEventListener("click", (event) => {
  if (myElementToCheckIfClicksAreInsideOf.contains(event.target)) {
    console.log("clicked inside");
  } else {
    console.log("clicked outside");
  }
});

to add a click listener for the myElementToCheckIfClicksAreInsideOf container element with addEventListener.

In it we call myElementToCheckIfClicksAreInsideOf.contains to check if the element that we clicked on is in myElementToCheckIfClicksAreInsideOf .

event.target is the element that we clicked on.

Conclusion

To detect click inside/outside of element with single event handler with JavaScript, we use the element contains method.

Categories
JavaScript Answers

How to detect if user is using web view for Android/iOS or a regular browser with JavaScript?

Sometimes, we want to detect if user is using web view for Android/iOS or a regular browser with JavaScript.

In this article, we’ll look at how to detect if user is using web view for Android/iOS or a regular browser with JavaScript.

How to detect if user is using web view for Android/iOS or a regular browser with JavaScript?

To detect if user is using web view for Android/iOS or a regular browser with JavaScript, we use the user agent string.

For instance, we write

const userAgent = window.navigator.userAgent.toLowerCase();
const safari = /safari/.test(userAgent);
const ios = /iphone|ipod|ipad/.test(userAgent);

if (ios) {
  if (safari) {
    //...
  } else if (!safari) {
    //...
  }
} else {
  //...
}

to get the user agent string with window.navigator.userAgent.

We return a all lower case version it with toLowerCase.

Then we check if the user if using Safari with /safari/.test(userAgent)

And we check if the user if using iOS with /iphone|ipod|ipad/.test(userAgent).

Conclusion

To detect if user is using web view for Android/iOS or a regular browser with JavaScript, we use the user agent string.

Categories
JavaScript Answers

How to pass a URL with multiple parameters into a URL with JavaScript?

Sometimes, we want to pass a URL with multiple parameters into a URL with JavaScript.

In this article, we’ll look at how to pass a URL with multiple parameters into a URL with JavaScript.

How to pass a URL with multiple parameters into a URL with JavaScript?

To pass a URL with multiple parameters into a URL with JavaScript, we use the encodeURIComponent method.

For instance, we write

const encodedParam = encodeURIComponent(
  "www.foobar.com/?first=1&second=12&third=5"
);

to call encodeURIComponent with the URL string.

Then URL encoded string is returned.

Conclusion

To pass a URL with multiple parameters into a URL with JavaScript, we use the encodeURIComponent method.

Categories
JavaScript Answers

How to use address instead of longitude and latitude with Google Maps API and JavaScript?

Sometimes, we want to use address instead of longitude and latitude with Google Maps API and JavaScript.

In this article, we’ll look at how to use address instead of longitude and latitude with Google Maps API and JavaScript.

How to use address instead of longitude and latitude with Google Maps API and JavaScript?

To use address instead of longitude and latitude with Google Maps API and JavaScript, we can use geocoding.

For instance, we write

const addressesArray = [
  "Address Str.No, Postal Area/city",
  //...
];
const map = new google.maps.Map(document.getElementById("map"), {
  center: {
    lat: 12.7826,
    lng: 105.0282,
  },
  zoom: 6,
  gestureHandling: "cooperative",
});

const geocoder = new google.maps.Geocoder();
for (const address of addressArray) {
  geocoder.geocode(
    {
      address,
    },
    (results, status) => {
      if (status === "OK") {
        const marker = new google.maps.Marker({
          map,
          position: results[0].geometry.location,
          center: {
            lat: 12.7826,
            lng: 105.0282,
          },
        });
      } else {
        cinsole.log(
          "Geocode was not successful for the following reason: ",
          status
        );
      }
    }
  );
}

to create a map with the google.maps.Map constructor.

Then we create a Geocoder object.

Next we loop through the addressArray with a for-of loop.

In it, we call geocoder.geocode with an object with the address property to to get the location with results[0].geometry.location.

Conclusion

To use address instead of longitude and latitude with Google Maps API and JavaScript, we can use geocoding.

Categories
JavaScript Answers

How to rotate image with JavaScript?

Sometimes, we want to rotate image with JavaScript.

In this article, we’ll look at how to rotate image with JavaScript.

How to rotate image with JavaScript?

To rotate image with JavaScript, we set the transform property.

For instance, we write

document.getElementById("image").style.transform = "rotate(90deg)";

to select the image with getElementById.

Then we set its style.transform property to "rotate(90deg)" to rotate the image by 90 degrees.

Conclusion

To rotate image with JavaScript, we set the transform property.