Categories
JavaScript Answers

How to check if element exists using Cypress.io and JavaScript?

Sometimes, we want to check if element exists using Cypress.io and JavaScript.

In this article, we’ll look at how to check if element exists using Cypress.io and JavaScript.

How to check if element exists using Cypress.io and JavaScript?

To check if element exists using Cypress.io and JavaScript, we use the find method.

For instance, we write

const $body = await cy.get("body");
if ($body.find("button[data-cy=appDrawerOpener]").length > 0) {
  //...
}

to call the get method to get the body element.

Then we check if a button with data-cy attribute set to appDrawerOpener in the body element with $body.find.

If it returns length bigger than 0, then it’s found.

Conclusion

To check if element exists using Cypress.io and JavaScript, we use the find method.

Categories
JavaScript Answers

How to load file with Node.js and JavaScript?

Sometimes, we want to load file with Node.js and JavaScript.

In this article, we’ll look at how to load file with Node.js and JavaScript.

How to load file with Node.js and JavaScript?

To load file with Node.js and JavaScript, we use the readFile method.

For instance, we write

const fs = require("fs");
fs.readFile(__dirname + "/test.txt", (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data.toString());
});

to call fs.readFile with the path to the file.

And we get the file’s content from the data parameter in the callback.

Conclusion

To load file with Node.js and JavaScript, we use the readFile method.

Categories
JavaScript Answers

How to display JavaScript variables in a HTML page without document.write?

Sometimes, we want to display JavaScript variables in a HTML page without document.write.

In this article, we’ll look at how to display JavaScript variables in a HTML page without document.write.

How to display JavaScript variables in a HTML page without document.write?

To display JavaScript variables in a HTML page without document.write, we set the element’s innerHTML property.

For instance, we write

<div class="results"></div>

to add a div.

Then we write

document.querySelector(".results").innerHTML = "Hello World!";

to select the div with querySelector.

And then we set its innerHTML property to the content we want to display in it.

Conclusion

To display JavaScript variables in a HTML page without document.write, we set the element’s innerHTML property.

Categories
JavaScript Answers

How to format fate as “yyyy-MM-dd’T’HH:mm:ss.SSS’Z'” with JavaScript?

To format fate as "yyyy-MM-dd’T’HH:mm:ss.SSS’Z’" with JavaScript, we use the toISOString method.

For instance, we write

const dt = new Date("30 July 2022 15:05 UTC");
console.log(dt.toISOString());

to create a new Date object.

Then we call toISOString on it to return a date time string in the "yyyy-MM-dd’T’HH:mm:ss.SSS’Z’" format.

Categories
JavaScript Answers

How to determine if user selected a file for file upload with JavaScript?

Sometimes, we want to determine if user selected a file for file upload with JavaScript.

In this article, we’ll look at how to determine if user selected a file for file upload with JavaScript.

How to determine if user selected a file for file upload with JavaScript?

To determine if user selected a file for file upload with JavaScript, we check if the value property is not an empty string.

For instance, we write

if (document.getElementById("uploadBox").value !== "") {
  // ...
}

to select the file input with getElementById.

And if its value isn’t an empty string, then a file is selected.

Conclusion

To determine if user selected a file for file upload with JavaScript, we check if the value property is not an empty string.