Categories
JavaScript Answers

How to toggle visibility property of a div with JavaScript?

Sometimes, we want to toggle visibility property of a div with JavaScript.

In this article, we’ll look at how to toggle visibility property of a div with JavaScript.

How to toggle visibility property of a div with JavaScript?

To toggle visibility property of a div with JavaScript, we set the style.visibility property.

For instance, we write

const toggleVideo = () => {
  const e = document.getElementById("video-over");
  if (e.style.visibility === "visible") {
    e.style.visibility = "hidden";
  } else if (e.style.visibility === "hidden") {
    e.style.visibility = "visible";
  }
};

to define the toggleVideo function.

In it, we get the element with getElementById.

Then we check if it’s visible with e.style.visibility === "visible".

If it’s true, we set it toi 'hidden'.

Otherwise, we set it to 'visible'.

Conclusion

To toggle visibility property of a div with JavaScript, we set the style.visibility property.

Categories
JavaScript Answers

How to get element by a custom attribute using JavaScript?

Sometimes, we want to get element by a custom attribute using JavaScript.

In this article, we’ll look at how to get element by a custom attribute using JavaScript.

How to get element by a custom attribute using JavaScript?

To get element by a custom attribute using JavaScript, we use querySelector.

For instance, we write

const element = document.querySelector('[tokenid="14"]');

to get the element with tokenid attribute set to 14 with querySelector.

Conclusion

To get element by a custom attribute using JavaScript, we use querySelector.

Categories
JavaScript Answers

How to dynamically remove a stylesheet from the current page with JavaScript?

Sometimes, we want to dynamically remove a stylesheet from the current page with JavaScript.

in this article, we’ll look at how to dynamically remove a stylesheet from the current page with JavaScript.

How to dynamically remove a stylesheet from the current page with JavaScript?

To dynamically remove a stylesheet from the current page with JavaScript, we call the remove method.

For instance, we write

document.querySelector('link[href$="something.css"]').remove();

to select the link element with href attribute that includes something.css with querySelector.

Then we call remove to remove the element from the DOM.

Conclusion

To dynamically remove a stylesheet from the current page with JavaScript, we call the remove method.

Categories
JavaScript Answers

How to fix JavaScript’s getDate returns wrong date?

Sometimes, we want to fix JavaScript’s getDate returns wrong date.

In this article, we’ll look at how to fix JavaScript’s getDate returns wrong date.

How to fix JavaScript’s getDate returns wrong date?

To fix JavaScript’s getDate returns wrong date, we use the Date constructor.

For instance, we write

const parseDate = (input) => {
  const [year, month, day] = input.match(/(\d+)/g);
  return new Date(year, month - 1, day);
};

to call input.match to get the parts of a date in an array.

Then we call the Date constructor with the year, month and day.

We subtract month by 1 since Date accepts month number from 0 for January to 11 for December.

Conclusion

To fix JavaScript’s getDate returns wrong date, we use the Date constructor.

Categories
JavaScript Answers

How to use FormData in Node.js without browser with JavaScript?

To use FormData in Node.js without browser with JavaScript, we use the form-data module.

To install it, we run

npm i form-data

Then we write

const FormData = require("form-data");
const fs = require("fs");

const form = new FormData();
form.append("foo", "my value");
form.append("bar", new Buffer(10));
form.append("file", fs.createReadStream("/foo/bar.jpg"));

to create a FormData object.

We call append with the key and value of each entry.

And we set the value to a file by creating a read stream with createReadStream.