Categories
JavaScript Answers

How to use anchor tags in react-router 4 and JavaScript?

Sometimes, we want to use anchor tags in react-router 4 and JavaScript.

In this article, we’ll look at how to use anchor tags in react-router 4 and JavaScript.

How to use anchor tags in react-router 4 and JavaScript?

To use anchor tags in react-router 4 and JavaScript, we use the eact-router-hash-link package.

To install it, we run

npm install --save react-router-hash-link

Then we use it by writing

import { HashLink as Link } from "react-router-hash-link";

<Link to="/pathLink#yourAnchorTag">Your link text</Link>;

to import the HashLink component as Link.

And then we use it with the to prop set to the destination URL.

Conclusion

To use anchor tags in react-router 4 and JavaScript, we use the eact-router-hash-link package.

Categories
JavaScript Answers

How to set dropdown selected item based on option text with JavaScript?

To set dropdown selected item based on option text with JavaScript, we set the selectedIndex property of the select drop down.

For instance, we write

<select id="myDropDown">
  <option value="0">Google</option>
  <option value="1">Bing</option>
  <option value="2">Yahoo</option>
</select>

to add the drop down.

Then we write

const textToFind = "Bing";
const dd = document.getElementById("myDropDown");
dd.selectedIndex = [...dd.options].findIndex(
  (option) => option.text === textToFind
);

to get the select drop down with getElementById.

Then we find the element with the options we want to set with findIndex.

We get the node list of option elements with dd.options and spread the entries into an array before using findIndex.

option.text has the option text.

Categories
JavaScript Answers

How to compare two times with Moment.js and JavaScript?

Sometimes, we want to compare two times with Moment.js and JavaScript.

In this article, we’ll look at how to compare two times with Moment.js and JavaScript.

How to compare two times with Moment.js and JavaScript?

To compare two times with Moment.js and JavaScript, we can parse times with moment.

For instance, we write

const beginningTime = moment("8:45am", "h:mma");
const endTime = moment("9:00am", "h:mma");
console.log(beginningTime.isBefore(endTime));

to call moment with the times we want to compare.

Then we check if beginningTime is before endTime with isBefore.

Conclusion

To compare two times with Moment.js and JavaScript, we can parse times with moment.

Categories
JavaScript Answers

How to get coordinates of marker in Google Maps API and JavaScript?

Sometimes, we want to get coordinates of marker in Google Maps API and JavaScript.

In this article, we’ll look at how to get coordinates of marker in Google Maps API and JavaScript.

How to get coordinates of marker in Google Maps API and JavaScript?

To get coordinates of marker in Google Maps API and JavaScript, we listen for the dragend event.

For instance, we write

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

const myMarker = new google.maps.Marker({
  position: new google.maps.LatLng(41, -100),
  draggable: true,
});

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

map.setCenter(myMarker.position);
myMarker.setMap(map);

to listen for the dragend event of the myMarker marker to get its coordinates after dragging.

We get its latitude with evt.latLng.lat.

And we get its latitude with evt.latLng.lng.

Conclusion

To get coordinates of marker in Google Maps API and JavaScript, we listen for the dragend event.

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.