Categories
JavaScript Answers

How to hide labels on y axis in Chart.js with JavaScript?

Sometimes, we want to hide labels on y axis in Chart.js with JavaScript.

In this article, we’ll look at how to hide labels on y axis in Chart.js with JavaScript.

How to hide labels on y axis in Chart.js with JavaScript?

To hide labels on y axis in Chart.js with JavaScript, we set the display property.

For instance, we write

const ctx = document.getElementById("log");
const chart = new Chart(ctx, {
  type: "line",
  options: {
    scales: {
      xAxes: [
        {
          display: false,
        },
      ],
      yAxes: [
        {
          display: false,
        },
      ],
    },
  },
  data: {
    labels: ["Item 1", "Item 2", "Item 3"],
    datasets: [
      {
        fill: false,
        borderWidth: 1,
        data: [10, 20, 30],
      },
    ],
  },
});

to create a chart with the Chart constructor by calling it with an object that sets the options.scale.xAxes to an array with an object that has display set to false to hide the x-axis labels.

Likewise, we do the same with yAxes to hide the y-axis labels.

Conclusion

To hide labels on y axis in Chart.js with JavaScript, we set the display property.

Categories
JavaScript Answers

How to find first index of a string after index with JavaScript?

Sometimes, we want to find first index of a string after index with JavaScript.

In this article, we’ll look at how to find first index of a string after index with JavaScript.

How to find first index of a string after index with JavaScript?

To find first index of a string after index with JavaScript, we call indexOf with the 2nd argument.

For instance, we write

const index = str.indexOf("s", startIndex);

to call indexOf to find the first 's' at startIndex or after.

Conclusion

To find first index of a string after index with JavaScript, we call indexOf with the 2nd argument.

Categories
JavaScript Answers

How to get the selected radio button value using JavaScript?

Sometimes, we want to get the selected radio button value using JavaScript.

In this article, we’ll look at how to get the selected radio button value using JavaScript.

How to get the selected radio button value using JavaScript?

To get the selected radio button value using JavaScript, we use the :checked pseudoselector.

For instance, we write

<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male" />Male<br />
<input type="radio" id="gender1" name="gender" value="Female" />Female<br />

to add radio buttons.

Then we write

const gender = document.querySelector('input[name = "gender"]:checked').value;

to select the selected radio button with name attribute gender with querySelector.

And we get its value with value.

Conclusion

To get the selected radio button value using JavaScript, we use the :checked pseudoselector.

Categories
JavaScript Answers

How to use regular expression for formatting numbers with thousands separators in JavaScript?

Sometimes, we want to use regular expression for formatting numbers with thousands separators in JavaScript.

In this article, we’ll look at how to use regular expression for formatting numbers with thousands separators in JavaScript.

How to use regular expression for formatting numbers with thousands separators in JavaScript?

To use regular expression for formatting numbers with thousands separators in JavaScript, we use the string replace method.

For instance, we write

const result = subject.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

to replace every groups of 3 digits with the groups plus a comma after it.

Conclusion

To use regular expression for formatting numbers with thousands separators in JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to add zeros to the beginning of a string (max length 4 chars) with JavaScript?

Sometimes, we want to add zeros to the beginning of a string (max length 4 chars) with JavaScript.

In this article, we’ll look at how to add zeros to the beginning of a string (max length 4 chars) with JavaScript.

How to add zeros to the beginning of a string (max length 4 chars) with JavaScript?

To add zeros to the beginning of a string (max length 4 chars) with JavaScript, we use the string padStart method.

For instance, we write

const padded = numberStr.padStart(4, "0");

to call padStart with 4 and '0' to return a string with the numberStr value prepended with 0’s until its length is 4.

Conclusion

To add zeros to the beginning of a string (max length 4 chars) with JavaScript, we use the string padStart method.