Categories
JavaScript Answers

How to use HTML5 Canvas and JavaScript to take in-browser screenshots?

Sometimes, we want to use HTML5 Canvas and JavaScript to take in-browser screenshots.

In this article, we’ll look at how to use HTML5 Canvas and JavaScript to take in-browser screenshots.

How to use HTML5 Canvas and JavaScript to take in-browser screenshots?

To use HTML5 Canvas and JavaScript to take in-browser screenshots, we use the getDisplayMedia method.

For instance, we write

<video style="width: 100%; height: 100%; border: 1px black solid" />

to add a video element.

Then we write

const mediaStream = await navigator.mediaDevices.getDisplayMedia();
const video = document.querySelector("video");
video.srcObject = mediaStream;
video.onloadedmetadata = (e) => {
  video.play();
  video.pause();
};

to call getDisplayMedia to get permission for using the display.

Then we select the video element with querySelector.

And then we set the srcObject to the mediaStream that we get after permission is granted.

Then we set onloadedmetadata to a function that calls play to play the video and pause to pause it once the display is captured.

Conclusion

To use HTML5 Canvas and JavaScript to take in-browser screenshots, we use the getDisplayMedia method.

Categories
JavaScript Answers

How to retrieve an HTML element’s actual width and height with JavaScript?

Sometimes, we want to retrieve an HTML element’s actual width and height with JavaScript.

In this article, we’ll look at how to retrieve an HTML element’s actual width and height with JavaScript.

How to retrieve an HTML element’s actual width and height with JavaScript?

To retrieve an HTML element’s actual width and height with JavaScript, we use its offsetWidth property.

For instance, we write

const width = document.getElementById("foo").offsetWidth;

to select the element with getElementById.

Then we use its offsetWidth property to get its width in pixels.

Conclusion

To retrieve an HTML element’s actual width and height with JavaScript, we use its offsetWidth property.

Categories
JavaScript Answers

How to change an element’s class with JavaScript?

Sometimes, we want to change an element’s class with JavaScript.

In this article, we’ll look at how to change an element’s class with JavaScript.

How to change an element’s class with JavaScript?

To change an element’s class with JavaScript=, we use the classList methods.

For instance, we write

document.getElementById("MyElement").classList.add("MyClass");
document.getElementById("MyElement").classList.remove("MyClass");
document.getElementById("MyElement").classList.toggle("MyClass");

to select the element with getElementById.

Then we call classList.add to add MyClass to the element.

We call classList.remove to remove MyClass from element.

And we call classList.toggle to toggle MyClass on the element.

Conclusion

To change an element’s class with JavaScript=, we use the classList methods.

Categories
JavaScript Answers

How to strip HTML from text with JavaScript?

Sometimes, we want to strip HTML from text with JavaScript.

In this article, we’ll look at how to strip HTML from text with JavaScript.

How to strip HTML from text with JavaScript?

To strip HTML from text with JavaScript, we can put the HTML in an element and then get the textContent from the element.

For instance, we write

const stripHtml = (html) => {
  const tmp = document.createElement("DIV");
  tmp.innerHTML = html;
  return tmp.textContent || tmp.innerText || "";
};

to define the stripHtml function that takes the html string.

In it, we create a div with createElement.

And then we assign html to the tmp.innerHTML to populate the div.

Then we get the plain text from the textContent or innerText properties.

Conclusion

To strip HTML from text with JavaScript, we can put the HTML in an element and then get the textContent from the element.

Categories
JavaScript Answers

How to check if an element contains a class in JavaScript?

Sometimes, we want to check if an element contains a class in JavaScript.

In this article, we’ll look at how to check if an element contains a class in JavaScript.

How to check if an element contains a class in JavaScript?

To check if an element contains a class in JavaScript, we use the classList.contains method.

For instance, we write

const hasClass = element.classList.contains("class");

to call element.classList.contains with 'class' to check if element has the class class.

It’ll return true if it’s added and false otherwise.

Conclusion

To check if an element contains a class in JavaScript, we use the classList.contains method.