Categories
JavaScript Answers

How to make HTML form submit do nothing with JavaScript?

Sometimes, we want to make HTML form submit do nothing with JavaScript.

In this article, we’ll look at how to make HTML form submit do nothing with JavaScript.

How to make HTML form submit do nothing with JavaScript?

To make HTML form submit do nothing with JavaScript, we return false in the submit handler.

For instance, we write

<form onsubmit="return false;">
  <input type="submit" value="Submit" />
</form>

to set the onsubmit attribute to return false; to stop form submission when we click on the submit button.

Conclusion

To make HTML form submit do nothing with JavaScript, we return false in the submit handler.

Categories
JavaScript Answers

How to get LatLng from zip code with Google Maps API?

Sometimes, we want to get LatLng from zip code with Google Maps API.

In this article, we’ll look at how to get LatLng from zip code with Google Maps API.

How to get LatLng from zip code with Google Maps API?

To get LatLng from zip code with Google Maps API, we can use an endpoint to get the latlng from the zip code.

For instance, we make a get request to

https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:97403

to get get the latlng data from the 97403 zip code.

An API key is required to make this request.

Conclusion

To get LatLng from zip code with Google Maps API, we can use an endpoint to get the latlng from the zip code.

Categories
JavaScript Answers

How to get the background color code of an element in hex with JavaScript?

Sometimes, we want to get the background color code of an element in hex with JavaScript.

In this article, we’ll look at how to get the background color code of an element in hex with JavaScript.

How to get the background color code of an element in hex with JavaScript?

To get the background color code of an element in hex with JavaScript, we can set the strokeStyle property of the canvas and then retrieve the value.

For instance, we write

const ctx = document.createElement("canvas").getContext("2d");
ctx.strokeStyle = "rgb(64, 128, 192)";
const hexColor = ctx.strokeStyle;

to create a canvas with createElement.

Then we get its context with getContext.

Next, we set strokeStyle to a color string.

And then we get the hex color value with ctx.strokeStyle.

Conclusion

To get the background color code of an element in hex with JavaScript, we can set the strokeStyle property of the canvas and then retrieve the value.

Categories
JavaScript Answers

How to create date from year, month, day with JavaScript?

Sometimes, we want to create date from year, month, day with JavaScript.

In this article, we’ll look at how to create date from year, month, day with JavaScript.

How to create date from year, month, day with JavaScript?

To create date from year, month, day with JavaScript, we can use the Date constructor.

For instance, we write

const d = new Date(2022, 11, 17);

to call the Date constructor with the year, month, and day.

The month starts with 0 for January and ends with 11 for December.

Conclusion

To create date from year, month, day with JavaScript, we can use the Date constructor.

Categories
React Answers

How to display binary data as image in React?

Sometimes, we want to display binary data as image in React.

In this article, we’ll look at how to display binary data as image in React.

How to display binary data as image in React?

To display binary data as image in React, we can set the src prop of the img element to a base64 URL.

For instance, we write

const data =
  "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

const Example = ({ data }) => <img src={`data:image/jpeg;base64,${data}`} />;

to set the src prop of the img element in Example to a base64 URL with the binary image data.

Then the image will be displayed.

Conclusion

To display binary data as image in React, we can set the src prop of the img element to a base64 URL.