Categories
JavaScript Answers

How to get an image from API with JavaScript Fetch API?

Sometimes, we want to get an image from API with JavaScript Fetch API.

In this article, we’ll look at how to get an image from API with JavaScript Fetch API.

How to get an image from API with JavaScript Fetch API?

To get an image from API with JavaScript Fetch API, we can call the response’s blob method and use the FileReader to read the file into a base64 string.

For instance, we write:

const imageUrl = "https://picsum.photos/200/300";

const reader = new FileReader();
reader.onloadend = () => {
  const base64data = reader.result;                
  console.log(base64data);
}

(async () => {
  const response = await fetch(imageUrl)
  const imageBlob = await response.blob()
  reader.readAsDataURL(imageBlob);  
})()

We create the FileReader instance and set the onloadend property to a function that gets the base64 string from reader.result.

Next, we call fetch with the imageUrl to make a GET request to the image URL.

Then we call response.blob to return a promise with the image blob object.

Finally, we call readAsDataURL with imageBlob to read it into a base64 string.

Conclusion

To get an image from API with JavaScript Fetch API, we can call the response’s blob method and use the FileReader to read the file into a base64 string.

Categories
JavaScript Answers jQuery

How to get the total number of elements selected with jQuery?

Sometimes, we want to get the total number of elements selected with jQuery.

In this article, we’ll look at how to get the total number of elements selected with jQuery.

How to get the total number of elements selected with jQuery?

To get the total number of elements selected with jQuery, we can use the length property.

For instance, if we have:

<div>
  foo
</div>
<div>
  bar
</div>
<div>
  baz
</div>

Then we write:

console.log($('div').length)

to log the number of divs selected, which is 3.

Conclusion

To get the total number of elements selected with jQuery, we can use the length property.

Categories
JavaScript Answers

What is the analog of the Date.now() JavaScript method in Moment.js?

Sometimes, we want to use the analog of the Date.now() JavaScript method in Moment.js.

In this article, we’ll find the analog of the Date.now() JavaScript method in Moment.js.

What is the analog of the Date.now() JavaScript method in Moment.js?

The analog of the Date.now() JavaScript method in Moment.js is the moment().valueOf() method.

It returns the UNIX timestamp in milliseconds.

Categories
JavaScript Answers

How to submit a file input without submit button using JavaScript?

Sometimes, we want to submit a file input without submit button using JavaScript.

In this article, we’ll look at how to submit a file input without submit button using JavaScript.

How to submit a file input without submit button using JavaScript?

To submit a file input without submit button using JavaScript, we can call the form element’s submit method.

For instance, we write:

<form enctype="multipart/form-data" method="POST" action="/upload">
  <input id="fileInput" type="file" name="file">
  <input type="submit">
</form>

to add a form.

Then we write:

const fileInput = document.getElementById('fileInput')
const form = document.querySelector('form')

fileInput.addEventListener('change', () => {
  form.submit();
});

to select the file input and form with document.getElementById and document.querySelector respectivelt.

Next, we call fileInput.addEventListener to add a change listener to the file input.

In the change listener, we call form.submit to submit the form.

Now when we select a file, the form submission will be done immediately.

Conclusion

To submit a file input without submit button using JavaScript, we can call the form element’s submit method.

Categories
JavaScript Answers

How to check if one number is a multiple of another with JavaScript?

Sometimes, we want to check if one number is a multiple of another with JavaScript.

In this article, we’ll look at how to check if one number is a multiple of another with JavaScript.

How to check if one number is a multiple of another with JavaScript?

To check if one number is a multiple of another with JavaScript, we can use the % operator check if one number divided by the other has a remainder of 0.

For instance, we write:

const x = 8
const y = 2
const remainder = x % y;
if (remainder === 0) {
  //x is a multiple of y
} else {
  //x is not a multiple of y
}

We get the remainder of x divided by y with the % operator and assign the returned remainder to remainder.

If remainder is 0, then xis a multiple ofy`.

Otherwise, x isn’t a multiple of y.

Conclusion

To check if one number is a multiple of another with JavaScript, we can use the % operator check if one number divided by the other has a remainder of 0.