Categories
TypeScript Answers

How to watch and compile all TypeScript sources?

Sometimes, we want to watch and compile all TypeScript sources.

In this article, we’ll look at how to watch and compile all TypeScript sources.

How to watch and compile all TypeScript sources?

To watch and compile all TypeScript sources, we can make some changes to the tsconfig.json file in our TypeScript project.

For instance, we write

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "module": "commonjs",
    "target": "ES5",
    "outDir": "ts-built",
    "rootDir": "src"
  }
}

to set the rootDir property to the path of the folder with all the files we want to compile.

Conclusion

To watch and compile all TypeScript sources, we can make some changes to the tsconfig.json file in our TypeScript project.

Categories
JavaScript Answers

How to use two Y axes in Chart.js v2 and JavaScript?

Sometimes, we want to use two Y axes in Chart.js v2 and JavaScript.

In this article, we’ll look at how to use two Y axes in Chart.js v2 and JavaScript.

How to use two Y axes in Chart.js v2 and JavaScript?

To use two Y axes in Chart.js v2 and JavaScript, we can put the axes in the yAxes array property.

For instance, we write

const canvas = document.getElementById("chart");
new Chart(canvas, {
  type: "line",
  data: {
    labels: ["1", "2", "3", "4", "5"],
    datasets: [
      {
        label: "A",
        yAxisID: "A",
        data: [100, 50, 0],
      },
      {
        label: "B",
        yAxisID: "B",
        data: [1, 1, 1],
      },
    ],
  },
  options: {
    scales: {
      yAxes: [
        {
          id: "A",
          type: "linear",
          position: "left",
        },
        {
          id: "B",
          type: "linear",
          position: "right",
          ticks: {
            max: 1,
            min: 0,
          },
        },
      ],
    },
  },
});

to set yAxes to an array with the properties for each y-axis.

We set the type, position, and the min and max ticks values.

Conclusion

To use two Y axes in Chart.js v2 and JavaScript, we can put the axes in the yAxes array property.

Categories
JavaScript Answers

How to re-enable right click to let us inspect HTML elements in Chrome with JavaScript?

Sometimes, we want to re-enable right click to let us inspect HTML elements in Chrome with JavaScript.

In this article, we’ll look at how to re-enable right click to let us inspect HTML elements in Chrome with JavaScript.

How to re-enable right click to let us inspect HTML elements in Chrome with JavaScript?

To re-enable right click to let us inspect HTML elements in Chrome with JavaScript, we can set the oncontextmenu of elements in our page to null.

For instance, we can set the right click handler of the window to null by running

window.oncontextmenu = null;

in the console.

We can also run

const elements = document.getElementsByTagName("*");
for (const element of elements) {
  element.oncontextmenu = null;
}

to select all elements on the page with getElementByTagName.

Then we loop through each element in elements and set its oncontextmenu property to null.

Conclusion

To re-enable right click to let us inspect HTML elements in Chrome with JavaScript, we can set the oncontextmenu of elements in our page to null.

Categories
JavaScript Answers

How to copy output of a JavaScript variable to the clipboard?

Sometimes, we want to copy output of a JavaScript variable to the clipboard.

In this article, we’ll look at how to copy output of a JavaScript variable to the clipboard.

How to copy output of a JavaScript variable to the clipboard?

To copy output of a JavaScript variable to the clipboard, we can use the navigator.clipboard.writeText method.

For instance, we write

const copyToClipboard = (text) => {
  return navigator.clipboard.writeText(text);
};

to define the copyToClipboard function that takes the text string parameter.

In it, we call navigator.clipboard.writeText with text to copy the text string content into the clipboard.

Conclusion

To copy output of a JavaScript variable to the clipboard, we can use the navigator.clipboard.writeText method.

Categories
JavaScript Answers

How to fix TypeError: moment().tz is not a function with JavaScript?

Sometimes, we want to fix TypeError: moment().tz is not a function with JavaScript.

In this article, we’ll look at how to fix TypeError: moment().tz is not a function with JavaScript.

How to fix TypeError: moment().tz is not a function with JavaScript?

To fix TypeError: moment().tz is not a function with JavaScript, we should make sure that we’re importing moment-timezone before referencing tz.

We install moment-timezone by running

npm install moment-timezone --save

Then, we write

const moment = require('moment-timezone');

to import the moment-timezone module in our code.

Conclusion

To fix TypeError: moment().tz is not a function with JavaScript, we should make sure that we’re importing moment-timezone before referencing tz.