Categories
JavaScript Answers

How to create a set in JavaScript?

Sometimes, we want to create a set in JavaScript.

In this article, we’ll look at how to create a set in JavaScript.

How to create a set in JavaScript?

To create a set in JavaScript, we can use the Set constructor.

For instance, we write

const set = new Set();
set.add("red");
const hasRed = set.has("red");
set.delete("red");

to create a new set with the Set constructor.

Then we call add to add 'red' into the set.

Next we call has to check if 'red' is in the set.

And then we call delete to remove 'red' from the set.

Conclusion

To create a set in JavaScript, we can use the Set constructor.

Categories
JavaScript Answers

How to add or remove HTML inside div using JavaScript?

Sometimes, we want to add or remove HTML inside div using JavaScript.

In this article, we’ll look at how to add or remove HTML inside div using JavaScript.

How to add or remove HTML inside div using JavaScript?

To add or remove HTML inside div using JavaScript, we can use the insertAdjacentHTML method.

For instance, we write

const addRow = () => {
  const div = document.getElementById("content");
  div.insertAdjacentHTML("afterbegin", "PUT_HTML_HERE");
};

to define the addRow function.

In it, we call getElementById to get the element with ID content.

Then we call div.insertAdjacentHTML to put HTML at the 'afterbegin' position.

To add HTML after the div.

Conclusion

To add or remove HTML inside div using JavaScript, we can use the insertAdjacentHTML method.

Categories
JavaScript Answers

How to filter an array with a function that returns a promise with JavaScript?

Sometimes, we want to filter an array with a function that returns a promise with JavaScript.

In this article, we’ll look at how to filter an array with a function that returns a promise with JavaScript.

How to filtering an array with a function that returns a promise with JavaScript?

To filter an array with a function that returns a promise with JavaScript, we can use the Promise.all method with the array filter method.

For instance, we write

const filterPromise = async (values, fn) => {
  const booleans = await Promise.all(values.map(fn));
  const filtered = values.filter((_, i) => booleans[i]);
  return filtered;
};

to define the filterPromise function.

In it, we call Promise.all with an array of promises we get from values.map(fn).

Then we call values.filter with a callback that returns an array of filtered values according to the values of booleans[i].

Conclusion

To filter an array with a function that returns a promise with JavaScript, we can use the Promise.all method with the array filter method.

Categories
JavaScript Answers

How to determine original size of image cross browser with JavaScript?

Sometimes, we want to determine original size of image cross browser with JavaScript.

In this article, we’ll look at how to determine original size of image cross browser with JavaScript.

How to determine original size of image cross browser with JavaScript?

To determine original size of image cross browser with JavaScript, we get the dimensions from the image element’s onload callback.

For instance, we write

const newImg = new Image();

newImg.onload = () => {
  const { height, width } = newImg;
  console.log(width, height);
};

newImg.src = imgSrc;

to create an img element with the Image constructor.

Then we set its onload property to a function that gets the width and height of the image.

We then set the src property of the image to imgSrc which will trigger onload to run.

Conclusion

To determine original size of image cross browser with JavaScript, we get the dimensions from the image element’s onload callback.

Categories
JavaScript Answers

How to copy text from a div to clipboard with JavaScript?

Sometimes, we want to copy text from a div to clipboard with JavaScript.

In this article, we’ll look at how to copy text from a div to clipboard with JavaScript.

How to copy text from a div to clipboard with JavaScript?

To copy text from a div to clipboard with JavaScript, we can use the document.execCommand method.

For instance, we write

const range = document.createRange();
range.selectNode(document.getElementById("a"));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
window.getSelection().removeAllRanges();

to create the range object with createRange.

Then we call selectNode with the element we want to copy text from.

Next we call removeAllRanges to remove all existing selections.

Then we call addRange to add the new range.

We call execCommand with 'copy' to copy the element’s text into the clipboard.

Finally, we call removeAllRanges to remove the selected text.

Conclusion

To copy text from a div to clipboard with JavaScript, we can use the document.execCommand method.