Categories
JavaScript Answers

How to get the image before submitting the form with HTML input type=file and JavaScript?

Sometimes, we want to get the image before submitting the form with HTML input type=file and JavaScript.

In this article, we’ll look at how to get the image before submitting the form with HTML input type=file and JavaScript.

How to get the image before submitting the form with HTML input type=file and JavaScript?

To get the image before submitting the form with HTML input type=file and JavaScript, we use the files property.

For instance, we write

<input type="file" onchange="previewFile()" /><br />
<img src="" height="200" alt="Image preview..." />

to add a file input and an img element.

Then we write

const previewFile = () => {
  const preview = document.querySelector("img");
  const file = document.querySelector("input[type=file]").files[0];
  const reader = new FileReader();

  reader.onloadend = () => {
    preview.src = reader.result;
  };

  if (file) {
    reader.readAsDataURL(file);
  } else {
    preview.src = "";
  }
};

to select the img element and file input with querySelector.

We get the select file with document.querySelector("input[type=file]").files[0].

And we read the file into a base64 URL with readAsDataURL.

reader.result has the file as a base64 URL string.

Conclusion

To get the image before submitting the form with HTML input type=file and JavaScript, we use the files property.

Categories
JavaScript Answers

How to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript?

Sometimes, we want to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript.

In this article, we’ll look at how to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript.

How to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript?

To fix Cannot read property ‘getContext’ of null, using canvas with JavaScript, we should make sure the canvas element is loaded before we try to get it.

For instance, we write

<canvas id='canvas'></canvas>

to add a canvas element.

Then we write

const canvas = document.getElementById("canvas");

to select the canvas element with getElementById.

Conclusion

To fix Cannot read property ‘getContext’ of null, using canvas with JavaScript, we should make sure the canvas element is loaded before we try to get it.

Categories
JavaScript Answers

How to push an associative item into an array in JavaScript?

Sometimes, we want to push an associative item into an array in JavaScript.

In this article, we’ll look at how to push an associative item into an array in JavaScript.

How to push an associative item into an array in JavaScript?

To push an associative item into an array in JavaScript, we use an object.

For instance, we write

const obj = {};
const name = "name";
const val = 2;
obj[name] = val;
console.log(obj);

to create the object obj.

Then we add a property with key name and value val into obj with

obj[name] = val;

Conclusion

To push an associative item into an array in JavaScript, we use an object.

Categories
JavaScript Answers

How to return index value from filter method with JavaScript?

Sometimes, we want to return index value from filter method with JavaScript.

In this article, we’ll look at how to return index value from filter method with JavaScript.

How to return index value from filter method with JavaScript?

To return index value from filter method with JavaScript, we use the findIndex method.

For instance, we write

const data = [];
const fieldId = 5;
const index = data.findIndex((x) => x.id === fieldId);

to call data.findIndex with a callback to get the value with id set to fieldId.

The index of the first item in data that matches the condition will be returned.

Conclusion

To return index value from filter method with JavaScript, we use the findIndex method.

Categories
JavaScript Answers

How to convert ArrayBuffer to blob with JavaScript?

Sometimes, we want to convert ArrayBuffer to blob with JavaScript.

In this article, we’ll look at how to convert ArrayBuffer to blob with JavaScript.

How to convert ArrayBuffer to blob with JavaScript?

To convert ArrayBuffer to blob with JavaScript, we use the Blob constructor.

For instance, we write

const buffer = new ArrayBuffer(32);
const blob = new Blob([buffer]);

to create a Blob with the buffer by putting it in an array and using the array as the argument of Blob.

Conclusion

To convert ArrayBuffer to blob with JavaScript, we use the Blob constructor.