Categories
JavaScript Answers

How to add custom styles for info window with Google Maps API v3 and JavaScript?

Sometimes, we want to add custom styles for info window with Google Maps API v3 and JavaScript.

In this article, we’ll look at how to add custom styles for info window with Google Maps API v3 and JavaScript.

How to add custom styles for info window with Google Maps API v3 and JavaScript?

To add custom styles for info window with Google Maps API v3 and JavaScript, we set the boxStyle property.

For instance, we write

<div class="infobox-wrapper">
  <div id="infobox1">Infobox</div>
</div>

to add divs for the infobox.

Then we write

const infobox = new InfoBox({
  content: document.getElementById("infobox1"),
  disableAutoPan: false,
  maxWidth: 150,
  pixelOffset: new google.maps.Size(-140, 0),
  zIndex: null,
  boxStyle: {
    background:
      "url('http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/tipbox.gif') no-repeat",
    opacity: 0.75,
    width: "280px",
  },
  closeBoxMargin: "12px 4px 2px 2px",
  closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
  infoBoxClearance: new google.maps.Size(1, 1),
});

to create an infobox from the element with the InfoBox constructor.

We set content to the div.

And we set boxStyle to the custom styles.

We also set the margin for the close box with closeBoxMargin and the max width with maxWidth.

Conclusion

To add custom styles for info window with Google Maps API v3 and JavaScript, we set the boxStyle property.

Categories
JavaScript Answers

How to calculate percentage with JavaScript?

Sometimes, we want to calculate percentage with JavaScript.

In this article, we’ll look at how to calculate percentage with JavaScript.

How to calculate percentage with JavaScript?

To calculate percentage with JavaScript, we use arithmetic operators.

For instance, we write

const percentage = (partialValue, totalValue) => {
  return (100 * partialValue) / totalValue;
};

const totalActivities = 10;
const doneActivities = 2;

const result = percentage(doneActivities, totalActivities);

to define the percentage function that multiplies partialValue by 100 and then divide the product by totalValue to get the percentage.

Conclusion

To calculate percentage with JavaScript, we use arithmetic operators.

Categories
JavaScript Answers

How to check for null inline in JavaScript?

Sometimes, we want to check for null inline in JavaScript.

In this article, we’ll look at how to check for null inline in JavaScript.

How to check for null inline in JavaScript?

To check for null inline in JavaScript, we use the optional chaining operator.

For instance, we write

const example = { a: ["first", { b: 3 }, false] };

console.log(example?.a);
console.log(example?.b);

console.log(example?.a?.[0]);
console.log(example?.a?.[1]?.a);
console.log(example?.a?.[1]?.b);

to use the optional chaining ?. operator to return undefined if any property in the chain is null or undefined.

Conclusion

To check for null inline in JavaScript, we use the optional chaining operator.

Categories
JavaScript Answers

How to close Bootstrap dropdown after link click with HTML?

Sometimes, we want to close Bootstrap dropdown after link click with HTML.

In this article, we’ll look at how to close Bootstrap dropdown after link click with HTML.

How to close Bootstrap dropdown after link click with HTML?

To close Bootstrap dropdown after link click with HTML, we add the dropdown-toggle class.

For instance, we write

<a class="dropdown-toggle"></a>

to add the dropdown-toggle class to the link to close Bootstrap dropdown after link click with HTML.

Conclusion

To close Bootstrap dropdown after link click with HTML, we add the dropdown-toggle class.

Categories
JavaScript Answers

How to get width and height of an image with JavaScript FileReader?

Sometimes, we want to get width and height of an image with JavaScript FileReader.

In this article, we’ll look at how to get width and height of an image with JavaScript FileReader.

How to get width and height of an image with JavaScript FileReader?

To get width and height of an image with JavaScript FileReader, we use the Image constructor.

For instance, we write

<input type="file" name="profileImg" accept="image/*" class="input-file" />

to add a file input.

Then we write

const input = document.querySelector("input");
input.onchange = (event) => {
  const reader = new FileReader();

  reader.onload = () => {
    const image = new Image();
    image.src = reader.result;
    image.onload = () => {
      consoe.log(image.width);
    };
  };
  const [file] = event.target.files;
  reader.readAsDataURL(file);
};

to select the input with querySelector.

And then we set its onchange property to a function that creates a FileReader object.

We set its onload property to a function that gets the read file data.

In it, we create an image with the Image constructor.

And we set image.src to the read file result which is a base64 URL string.

Then image.width has the width.

We get the file from the input with event.target.files.

And we call readAsDataURL to read the file into the base64 URL.

Conclusion

To get width and height of an image with JavaScript FileReader, we use the Image constructor.