Categories
JavaScript Answers

How to capture div into image using html2canvas?

Sometimes, we want to capture div into image using html2canvas.

In this article, we’ll look at how to capture div into image using html2canvas.

How to capture div into image using html2canvas?

To capture div into image using html2canvas, we can use the html2canvas function to return a promise with the canvas with the captured element content.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js">
</script>

<div id='hello'>
  hello world
</div>

to add a div.

Then we write:

(async () => {
  const div = document.getElementById('hello')
  const canvas = await html2canvas(div)
  document.body.appendChild(canvas)
})()

We select the div with document.getElementById.

Then we call html2canvas with div to capture the div’s content into a canvas.

And finally, we call document.body.appendChild with canvas to attach the captured canvas into the body.

Now we should see an image version of the div displayed.

Conclusion

To capture div into image using html2canvas, we can use the html2canvas function to return a promise with the canvas with the captured element content.

Categories
JavaScript Answers

How to read line by line of a text area HTML element?

Sometimes, we want to read line by line of a text area HTML element.

In this article, we’ll look at how to read line by line of a text area HTML element.

How to read line by line of a text area HTML element?

To read line by line of a text area HTML element, we can use the value property of the text area.

For instance, we write:

<textarea></textarea>

to add the text area.

Then we write:

const textarea = document.querySelector('textarea')

textarea.addEventListener('change', () => {
  const arrayOfLines = textarea.value.split("\n");
  for (const a of arrayOfLines) {
    console.log(a)
  }
})

We select the textarea with document.querySelector.

Then we call addEventListener to listen to the change event.

In the change event listener, we get the entered value string with textarea.value.

Then we call split with '\n' to split the string by the newline character into an array of strings.

And then we loop through each line with the for-of loop.

Conclusion

To read line by line of a text area HTML element, we can use the value property of the text area.

Categories
JavaScript Answers

How to check if file input is empty in JavaScript?

Sometimes, we want to check if file input is empty in JavaScript.

In this article, we’ll look at how to check if file input is empty in JavaScript.

How to check if file input is empty in JavaScript?

To check if file input is empty in JavaScript, we can get the files.length property from the file input.

For instance, we write:

<input type='file' id='videoUploadFile'>

to add a file input.

Then we write:

if (document.getElementById("videoUploadFile").files.length === 0) {
  console.log("no files selected");
}

We select the file input with document.getElementById.

Then we use the files.length property to check if any files are selected.

If it returns 0, then no file is selected.

Conclusion

To check if file input is empty in JavaScript, we can get the files.length property from the file input.

Categories
JavaScript Answers

How to perform a curl request in JavaScript?

Sometimes, we want to perform a curl request in JavaScript.

In this article, we’ll look at how to perform a curl request in JavaScript.

How to perform a curl request in JavaScript?

To perform a curl request in JavaScript, we can use the fetch function.

For instance, we write:

(async () => {
  const res = await fetch('https://yesno.wtf/api')
  const answer = await res.json()
  console.log(answer)
})()

We call fetch with the URL we want to make the request to to make a GET request.

Then we call res.json to convert the response body object to JSON.

fetch and res.json both return promises so we need to use await to get the resolved value.

And then we log the result.

Conclusion

To perform a curl request in JavaScript, we can use the fetch function.

Categories
JavaScript Answers

How to test if a URL string is absolute or relative with JavaScript?

Sometimes, we want to test if a URL string is absolute or relative with JavaScript.

In this article, we’ll look at how to test if a URL string is absolute or relative with JavaScript.

How to test if a URL string is absolute or relative with JavaScript?

To test if a URL string is absolute or relative with JavaScript, we can use a regex.

For instance, we write:

const r = new RegExp('^(?:[a-z]+:)?//', 'i');
console.log(r.test('http://example.com'));
console.log(r.test('test'));

We create a regex with the RegExp constructor that checks if there’s a protocol before the rest of the URL.

^(?:[a-z]+:)?/ checks for the protocol.

Then we call test on it with some URL strings to test.

Therefore, the console log should return true and false respectively since only absolute URLs have the protocol in front of it.

Conclusion

To test if a URL string is absolute or relative with JavaScript, we can use a regex.