Categories
JavaScript Answers

How to change the value of the Bootstrap 4 file input with JavaScript?

Sometimes, we want to change the value of the Bootstrap 4 file input with JavaScript.

In this article, we’ll look at how to change the value of the Bootstrap 4 file input with JavaScript.

How to change the value of the Bootstrap 4 file input with JavaScript?

To change the value of the Bootstrap 4 file input with JavaScript, we can get the element with the value text and change it to the new value when a file is selected.

For instance, we write

document.querySelector(".custom-file-input").addEventListener("change", (e) => {
  const fileName = document.getElementById("myInput").files[0].name;
  const nextSibling = e.target.nextElementSibling;
  nextSibling.innerText = fileName;
});

to listen to the change event of the custom file input with addEventListener.

In the change event listener, we get the selected file’s name with

document.getElementById("myInput").files[0].name

Then we get the element with the custom file input’s value with

e.target.nextElementSibling

And we change the element’s innerText to the fileName to show the selected file’s name.

Conclusion

To change the value of the Bootstrap 4 file input with JavaScript, we can get the element with the value text and change it to the new value when a file is selected.

Categories
JavaScript Answers

How to get lat/lng from Google marker with JavaScript?

Sometimes, we want to get lat/lng from Google marker with JavaScript.

In this article, we’ll look at how to get lat/lng from Google marker with JavaScript.

How to get lat/lng from Google marker with JavaScript?

To get lat/lng from Google marker with JavaScript, we can listen to the marker’s dragstart and dragend events.

For instance, we write

<div id="map_canvas"></div>

to add a div for the map.

Then we set its size with

#map_canvas {
  width: 400px;
  height: 300px;
}

Then we create the map with a marker inside with

const map = new google.maps.Map(document.getElementById("map_canvas"), {
  zoom: 1,
  center: new google.maps.LatLng(45.137879, -82.836914),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
});

const myMarker = new google.maps.Marker({
  position: new google.maps.LatLng(47.651968, 9.478485),
  draggable: true,
});

google.maps.event.addListener(myMarker, "dragend", (evt) => {
  console.log(evt.latLng.lng().toFixed(3));
});

google.maps.event.addListener(myMarker, "dragstart", (evt) => {
  console.log("dragging marker");
});

We use the Map constructor to fill the div with the map contents.

And we set the default zoom and center of the map.

Next, we create a marker with the Marker constructor.

And then we call addListener to listen to the dragstart and dragend events of the marker.

In the dragend callback, we get the longitude of the marker’s current location with evt.latLng.lng().

Conclusion

To get lat/lng from Google marker with JavaScript, we can listen to the marker’s dragstart and dragend events.

Categories
JavaScript Answers

How to get the full height of a HTML element including border, padding and margin with JavaScript?

Sometimes, we want to get the full height of a HTML element including border, padding and margin with JavaScript.

In this article, we’ll look at how to get the full height of a HTML element including border, padding and margin with JavaScript.

How to get the full height of a HTML element including border, padding and margin with JavaScript?

To get the full height of a HTML element including border, padding and margin with JavaScript, we can use the getComputedStyle method.

For instance, we write

const el = document.querySelector("div");

let elHeight = el.offsetHeight;
elHeight += parseInt(
  window.getComputedStyle(el).getPropertyValue("margin-top")
);
elHeight += parseInt(
  window.getComputedStyle(el).getPropertyValue("margin-bottom")
);

console.log(elHeight);

to get the div with querySelector.

Then we get the height of it without the margins with offsetHeight.

Next, we call getComputedStyle with el to get the margin top and margin bottom values of the div.

And then we add them to elHeight.

Conclusion

To get the full height of a HTML element including border, padding and margin with JavaScript, we can use the getComputedStyle method.

Categories
JavaScript Answers

How to remove an event listener with Google Maps API v3?

Sometimes, we want to remove an event listener with Google Maps API v3.

In this article, we’ll look at how to remove an event listener with Google Maps API v3.

How to remove an event listener with Google Maps API v3?

To remove an event listener with Google Maps API v3, we can call the removeListener method.

For instance, we write

const listenerHandle = google.maps.event.addListener(
  map,
  "bounds_changed",
  () => {
    //...
  }
);

google.maps.event.removeListener(listenerHandle);

to call addListener to add the bound_changed event listener onto the map.

It returns the listenerHandle object that we call removeListener with to remove it when we don’t need to listen to the bounds_changed event on the map.

Conclusion

To remove an event listener with Google Maps API v3, we can call the removeListener method.

Categories
React Native Answers

How to resize image with React Native?

Sometimes, we want to resize image with React Native.

In this article, we’ll look at how to resize image with React Native.

How to resize image with React Native?

To resize image with React Native, we set the width and height of the image to percentages and set resizeMode to 'cover'.

For instance, we write

<View
  style={{
    width: 180,
    height: 200,
    aspectRatio: 1 * 1.4,
  }}
>
  <Image
    source={{ uri: item.image.url }}
    style={{
      resizeMode: "cover",
      width: "100%",
      height: "100%",
    }}
  />
</View>;

to set the style of the Image to an object with resizeMode set to 'cover' to make the image fit the container.

We set the width and height to '100%' so it fills the whole container.

Conclusion

To resize image with React Native, we set the width and height of the image to percentages and set resizeMode to 'cover'.