Categories
JavaScript Answers

How to remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string?

Sometimes, we want to remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string.

In this article, we’ll look at how to remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string.

How to remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string?

To remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string, we can use the JavaScript string’s replace method.

For instance, we write:

const text = "A(B){C};:a.b*!c??!1<>2@#3"
const result = text.replace(/[^a-zA-Z0-9]/g, '')
console.log(result)

We call text.replace with a regex that matches all characters that aren’t letters and numbers and replace them with empty strings.

As a result, result is 'ABCabc123'.

Conclusion

To remove everything but letters, numbers, space, exclamation and question mark from a JavaScript string, we can use the JavaScript string’s replace method.

Categories
JavaScript Answers

How to read multiple files with JavaScript FileReader API one at a time?

Sometimes, we want to read multiple files with JavaScript FileReader API one at a time.

In this article, we’ll look at how to read multiple files with JavaScript FileReader API one at a time.

How to read multiple files with JavaScript FileReader API one at a time?

To read multiple files with JavaScript FileReader API one at a time, we can map the files to promises that resolves to the file content.

For instance, we write:

<input type='file' multiple>

to add a file input that accepts multiple files.

Then we write:

const upload = async (event) => {
  const files = [...event.target.files].map(file => {
    const reader = new FileReader();
    return new Promise(resolve => {
      reader.onload = () => resolve(reader.result);
      reader.readAsText(file);
    });
  });
  const res = await Promise.all(files);
  console.log(res)
}

const input = document.querySelector('input')
input.onchange = upload

We define the upload function that takes the selected files which are stored in event.target.files and spread them to an array.

Then we call map on the array with a callback that returns a promise that reads the file into a string with reader.readAsText and resolves to the read file which is stored in reader.result.

Next, we call Promise.all with the files promise array to get the file contents.

Then we select the input with document.querySelector.

And we set input.onchange to upload.

Now when we select files with the file input, we should see the contents of each file logged.

Conclusion

To read multiple files with JavaScript FileReader API one at a time, we can map the files to promises that resolves to the file content.

Categories
JavaScript Answers

How to get the image src value with jQuery and JavaScript?

Sometimes, we want to get the image src value with jQuery and JavaScript.

In this article, we’ll look at how to get the image src value with jQuery and JavaScript.

How to get the image src value with jQuery and JavaScript

To format a number to get the image src value with jQuery and JavaScript, we can use the prop method.

For instance, if we have the following img element:

<img src='https://i.picsum.photos/id/96/200/300.jpg?hmac=q257RPq4_aD8wno1Mkb4eP37WQzxDcNNLPu_HBwKdag'>

Then we can get the src attribute value with

console.log($('img').prop('src'))

Therefore, the console log logs 'https://i.picsum.photos/id/96/200/300.jpg?hmac=q257RPq4_aD8wno1Mkb4eP37WQzxDcNNLPu_HBwKdag'.

Conclusion

To format a number to get the image src value with jQuery and JavaScript, we can use the prop method.

Categories
JavaScript Answers

How to make multiple Axios requests at the same time with JavaScript?

Sometimes, we want to make multiple Axios requests at the same time with JavaScript.

In this article, we’ll look at how to make multiple Axios requests at the same time with JavaScript.

How to make multiple Axios requests at the same time with JavaScript?

To make multiple Axios requests at the same time with JavaScript, we can use the Promise.all method.

For instance, we write:

(async () => {
  const [{
    data: data1
  }, {
    data: data2
  }] = await Promise.all([
    axios.post('https://jsonplaceholder.typicode.com/posts'),
    axios.post('https://jsonplaceholder.typicode.com/posts'),
  ])
  console.log(data1, data2)
})()

We call axios.post to make POSTS requests and we put them both in an array.

Then we call Promise.all with the array to make the requests simultaneously.

Finally, we use the await keyword to run the promises and get the response from the data property.

Conclusion

To make multiple Axios requests at the same time with JavaScript, we can use the Promise.all method.

Categories
JavaScript Answers

How to check if all checkboxes are unchecked with JavaScript?

Sometimes, we want to check if all checkboxes are unchecked with JavaScript.

In this article, we’ll look at how to check if all checkboxes are unchecked with JavaScript.

How to check if all checkboxes are unchecked with JavaScript?

To check if all checkboxes are unchecked with JavaScript, we can use the JavaScript array every method.

For instance, we write:

<div>
  <input type='checkbox' name='foo'>
  <input type='checkbox' name='foo'>
</div>

to add 2 checkboxes.

Then we write:

const textinputs = document.querySelectorAll('input[type=checkbox]');
const empty = [...textinputs].every((el) => {
  return !el.checked
});
console.log(empty)

We select the checkboxes with:

const textinputs = document.querySelectorAll('input[type=checkbox]');

Next, we spread the textInputs into an array with the spread operator.

And then we call every to check if all the checkboxes are unchecked by returning !el.checked in the callback.

If all the checkboxes are unchecked, then empty should be true.

Conclusion

To check if all checkboxes are unchecked with JavaScript, we can use the JavaScript array every method.