Categories
JavaScript Answers

How to add disabled attribute to input element using JavaScript?

Sometimes, we want to add disabled attribute to input element using JavaScript.

In this article, we’ll look at how to add disabled attribute to input element using JavaScript.

How to add disabled attribute to input element using JavaScript?

To add disabled attribute to input element using JavaScript, we can set the disabled property of the select element to true.

For instance, we write

<select id="selectFrom">
  ...
</select>

to add the select element.

then we write

document.getElementById("selectFrom").disabled = true;

to get the select element with getElementById.

Then we set its disabled property to true to make it disabled.

Conclusion

To add disabled attribute to input element using JavaScript, we can set the disabled property of the select element to true.

Categories
JavaScript Answers

How to parse Excel (XLS) file in JavaScript and HTML5?

Sometimes, we want to parse Excel (XLS) file in JavaScript and HTML5.

In this article, we’ll look at how to parse Excel (XLS) file in JavaScript and HTML5.

How to parse Excel (XLS) file in JavaScript and HTML5?

To parse Excel (XLS) file in JavaScript and HTML5, we can use the read-excel-file library.

To install it, we run

npm install read-excel-file --save

Then we write

import readXlsxFile from "read-excel-file";

const input = document.getElementById("input");

input.addEventListener("change", async () => {
  const data = await readXlsxFile(input.files[0]);
});

to select the input with getElementById

Then we call addEventListener on it with 'change' and a callback that calls readXlsxFile to read the select file.

We get the read data from data which is an array of rows with each row being an array of cells.

Conclusion

To parse Excel (XLS) file in JavaScript and HTML5, we can use the read-excel-file library.

Categories
JavaScript Answers

How to format date and subtract days using Moment.js and JavaScript?

Sometimes, we want to format date and subtract days using Moment.js and JavaScript.

In this article, we’ll look at how to format date and subtract days using Moment.js and JavaScript.

How to format date and subtract days using Moment.js and JavaScript?

To format date and subtract days using Moment.js and JavaScript, we call format to format dates and subtract to subtract days.

For instance, we write

let startdate = moment();
startdate = startdate.subtract(1, "days");
startdate = startdate.format("DD-MM-YYYY");

to call subtract to subtract 1 day from the current date.

And then we call format to return a date string in DD-MM-YYYY format.

Conclusion

To format date and subtract days using Moment.js and JavaScript, we call format to format dates and subtract to subtract days.

Categories
JavaScript Answers

How to unit test a Node.js module that requires other modules and how to mock the global require function?

Sometimes, we want to unit test a Node.js module that requires other modules and how to mock the global require function.

In this article, we’ll look at how to unit test a Node.js module that requires other modules and how to mock the global require function.

How to unit test a Node.js module that requires other modules and how to mock the global require function?

To unit test a Node.js module that requires other modules and how to mock the global require function, we can use proxyquire.

To install it, we run

npm i proxyquire

Then we use it by writing

const proxyquire = require("proxyquire");
const assert = require("assert");
const pathStub = {};

//...

const foo = proxyquire("./foo", { path: pathStub });
assert.strictEqual(foo.bar("file.txt"), ".TXT");

to call proxyquire with the path to the module.

Then we call foo.bar from the foo module and check its returned value.

Conclusion

To unit test a Node.js module that requires other modules and how to mock the global require function, we can use proxyquire.

Categories
JavaScript Answers

How to capture key press or keydown event on a div element with JavaScript?

Sometimes, we want to capture key press or keydown event on a div element with JavaScript.

In this article, we’ll look at how to capture key press or keydown event on a div element with JavaScript.

How to capture key press or keydown event on a div element with JavaScript?

To capture key press or keydown event on a div element with JavaScript, we listen to the keyup event.

For instance, we write

document.querySelector("#myDiv").addEventListener("keyup", (e) => {
  console.log(e.key);
});

to select the div with document.querySelector("#myDiv").

Then we call addEventListener on it with 'keyup' and a function that runs when we release the key that’s pressed.

In it, we get the key released with e.key.

Conclusion

To capture key press or keydown event on a div element with JavaScript, we listen to the keyup event.