Categories
JavaScript Answers

How to get document height and width with JavaScript?

Sometimes, we want to get document height and width with JavaScript.

In this article, we’ll look at how to get document height and width with JavaScript.

How to get document height and width with JavaScript?

To get document height and width with JavaScript, we can use the clientWidth and clientHeight properties.

For instance, we write

const height = document.body.clientHeight;
const width = document.body.clientWidth;

to get the width and height of the body element with clientWidth and clientHeight respectively.

We use document.body to get the body element.

Conclusion

To get document height and width with JavaScript, we can use the clientWidth and clientHeight properties.

Categories
JavaScript Answers

How to save JSON to local text file with JavaScript?

Sometimes, we want to save JSON to local text file with JavaScript.

In this article, we’ll look at how to save JSON to local text file with JavaScript.

How to save JSON to local text file with JavaScript?

To save JSON to local text file with JavaScript. we can create a blob from the text.

For instance, we write

const a = document.createElement("a");
const file = new Blob([content], { type: "text/plain" });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();

to create a link with createElement.

Then we create a blob with the content string with the Blob constructor.

We set its MIME type to 'text/plain'.

And then we create an object URL from the blob with createObjectURL.

Next, we set the download property to a string with the name of the downloaded file.

Then we call click to download the content text into a file.

Conclusion

To save JSON to local text file with JavaScript. we can create a blob from the text.

Categories
JavaScript Answers

How to convert a Buffer into a ReadableStream in Node.js?

Sometimes, we want to convert a Buffer into a ReadableStream in Node.js.

In this article, we’ll look at how to convert a Buffer into a ReadableStream in Node.js.

How to convert a Buffer into a ReadableStream in Node.js?

To convert a Buffer into a ReadableStream in Node.js, we call the Readable.from method.

For instance, we write

const { Readable } = require("stream");

const stream = Readable.from(myBuffer.toString());

to call Readable.from with the myBuffer buffer converted to a string to convert myBuffer to a readable stream.

Conclusion

To convert a Buffer into a ReadableStream in Node.js, we call the Readable.from method.

Categories
JavaScript Answers

How to simulate a hover with a touch in touch enabled browsers with JavaScript?

Sometimes, we want to simulate a hover with a touch in touch enabled browsers with JavaScript.

In this article, we’ll look at how to simulate a hover with a touch in touch enabled browsers with JavaScript.

How to simulate a hover with a touch in touch enabled browsers with JavaScript?

To simulate a hover with a touch in touch enabled browsers with JavaScript, we add a touchstart event listener and some styles.

For instance, we write

document.addEventListener("touchstart", () => {}, true);

to add a touchstart event listener to document.

Then we write

element:hover,
element:active {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

to set the touch highlight color with

-webkit-tap-highlight-color: rgba(0, 0, 0, 0);

And we disable context menu on long press on the element with

-webkit-touch-callout: none

Conclusion

To simulate a hover with a touch in touch enabled browsers with JavaScript, we add a touchstart event listener and some styles.

Categories
JavaScript Answers

How to find HTML label associated with a given input with JavaScript?

Sometimes, we want to find HTML label associated with a given input with JavaScript.

In this article, we’ll look at how to find HTML label associated with a given input with JavaScript.

How to find HTML label associated with a given input with JavaScript?

To find HTML label associated with a given input with JavaScript, we can select the label elements and then loop through them to find the one with the htmlFor property equally the ID of the input.

For instance, we write

const findLableForControl = (el) => {
  const idVal = el.id;
  const labels = document.getElementsByTagName("label");
  for (const label of labels) {
    if (label.htmlFor === idVal) {
      return label;
    }
  }
};

to define the findLableForControl function that takes the el input element.

In it, we get the id of the el element.

Then we select all the labels with getElementsByTagName.

Next we loop through the labels and find the one with the htmlFor property equaling idVal.

htmlFor has the value of the for attribute of the label.

Conclusion

To find HTML label associated with a given input with JavaScript, we can select the label elements and then loop through them to find the one with the htmlFor property equally the ID of the input.