Categories
JavaScript Answers

How to return HTML with fetch() and JavaScript?

Sometimes, we want to return HTML with fetch() and JavaScript.

In this article, we’ll look at how to return HTML with fetch() and JavaScript.

How to return HTML with fetch() and JavaScript?

To return HTML with fetch() and JavaScript, we can use the response text method.

For instance, we write

const fetchMyDocument = async () => {
  try {
    const response = await fetch("/path/to/file.html");
    document.body.innerHTML = await response.text();
  } catch (err) {
    console.log(err);
  }
};

to call fetch to make a GET request to "/path/to/file.html".

Then we get the response from the promise returned by fetch.

Finally, we get the response body HTML from the promise returned by the response.text method with await.

Conclusion

To return HTML with fetch() and JavaScript, we can use the response text method.

Categories
JavaScript Answers

How to copy to clipboard using JavaScript in iOS?

Sometimes, we want to copy to clipboard using JavaScript in iOS.

In this article, we’ll look at how to copy to clipboard using JavaScript in iOS.

How to copy to clipboard using JavaScript in iOS?

To copy to clipboard using JavaScript in iOS, we use the input.setSelectionRange method.

For instance, we write

const copyText = (input) => {
  const isIOSDevice = navigator.userAgent.match(/ipad|iphone/i);

  if (isIOSDevice) {
    input.setSelectionRange(0, input.value.length);
  } else {
    input.select();
  }

  document.execCommand("copy");
};

to define the copyText function.

In it, we check if the function is run on an iOS device by checking the user agent with navigator.userAgent.

We check if 'iPad' or 'iPhone' is in the user agent string.

If it is, we call input.setSelectionRange to select the input’s text.

Otherwise, we use input.select to do the same thing.

Then we call document.execCommand with 'copy' to copy the selected text to the clipboard.

Conclusion

To copy to clipboard using JavaScript in iOS, we use the input.setSelectionRange method.

Categories
JavaScript Answers

How to get column from a two dimensional array with JavaScript?

Sometimes, we want to get column from a two dimensional array with JavaScript.

In this article, we’ll look at how to get column from a two dimensional array with JavaScript.

How to get column from a two dimensional array with JavaScript?

To get column from a two dimensional array with JavaScript, we can use the array map method.

For instance, we write

const arrayColumn = (arr, n) => arr.map((x) => x[n]);

const twoDimensionalArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(arrayColumn(twoDimensionalArray, 0));

to define the arrayColumn function that return an array with the items from column n in the 2D array arr.

We call map with a callback that returns the item at index n of each array in arr.

Then we call arrayColumn with twoDimensionalArray and 0 to get [1, 4, 7].

Conclusion

To get column from a two dimensional array with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to check for duplicate strings in JavaScript array?

Sometimes, we want to check for duplicate strings in JavaScript array.

In this article, we’ll look at how to check for duplicate strings in JavaScript array.

How to check for duplicate strings in JavaScript array?

To check for duplicate strings in JavaScript array, we can use a set.

For instance, we write

const checkIfDuplicateExists = (arr) => {
  return new Set(arr).size !== arr.length;
};

const arr = ["a", "a", "b", "c"];

console.log(checkIfDuplicateExists(arr));

to define the checkIfDuplicateExists function to check if the arr converted to a set has the same size as the original arr array.

If they have the same size, then there’re no duplicate values in arr since sets can’t have duplicate values but arrays can.

Conclusion

To check for duplicate strings in JavaScript array, we can use a set.

Categories
JavaScript Answers

How to check if input date is equal to today’s date with JavaScript?

Sometimes, we want to check if input date is equal to today’s date with JavaScript.

In this article, we’ll look at how to check if input date is equal to today’s date with JavaScript.

How to check if input date is equal to today’s date with JavaScript?

To check if input date is equal to today’s date with JavaScript, we can com[are the dates’ timestamps.

For instance, we write

const inputDate = new Date("11/21/2022");
const todaysDate = new Date();

if (inputDate.setHours(0, 0, 0, 0) === todaysDate.setHours(0, 0, 0, 0)) {
  // ...
}

to compare the timestamps of inputDate and todaysDate after we call setHours on each to set their times to midnight.

When we use === to compare them, their timestamps are compared.

So if their timestamps are the same, then they’re the same date.

Conclusion

To check if input date is equal to today’s date with JavaScript, we can com[are the dates’ timestamps.