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.

Categories
React Answers

How to change the URL in react-router v4 without using Redirect or Link with JavaScript?

Sometimes, we want to change the URL in react-router v4 without using Redirect or Link with JavaScript.

In this article, we’ll look at how to change the URL in react-router v4 without using Redirect or Link with JavaScript.

How to change the URL in react-router v4 without using Redirect or Link with JavaScript?

To change the URL in react-router v4 without using Redirect or Link with JavaScript, we call the history.push method.

For instance, we write

this.props.history.push("/foo");

to call history.push with '/foo' to navigate to /foo.

Conclusion

To change the URL in react-router v4 without using Redirect or Link with JavaScript, we call the history.push method.

Categories
JavaScript Answers

How to use ranges in a switch case statement using JavaScript?

Sometimes, we want to use ranges in a switch case statement using JavaScript.

In this article, we’ll look at how to use ranges in a switch case statement using JavaScript.

How to use ranges in a switch case statement using JavaScript?

To use ranges in a switch case statement using JavaScript, we use switch with true.

For instance, we write

switch (true) {
  case myInterval < 0:
    break;
  case myInterval >= 0 && myInterval <= 2:
    doStuffWithFirstRange();
    break;
  case myInterval >= 3 && myInterval <= 5:
    doStuffWithSecondRange();
    break;
  case myInterval >= 6 && myInterval <= 7:
    doStuffWithThirdRange();
    break;
  default:
    doStuffWithAllOthers();
}

to use switch with true.

Then we check the value of myInterval with the ranges the case expressions.

Conclusion

To use ranges in a switch case statement using JavaScript, we use switch with true.