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
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.

Categories
JavaScript Answers

How to convert IP to location using JavaScript?

Sometimes, we want to convert IP to location using JavaScript.

In this article, we’ll look at how to convert IP to location using JavaScript.

How to convert IP to location using JavaScript?

To convert IP to location using JavaScript, we can use https://ipstack.com/.

For instance, we make a request to

https://api.ipstack.com/160.39.144.19

to return a JSON response with the location of 160.39.144.19.

Conclusion.

To convert IP to location using JavaScript, we can use https://ipstack.com/.