Categories
JavaScript Answers

How to convert JavaScript date to string?

Sometimes, we want to convert JavaScript date to string.

In this article, we’ll look at how to convert JavaScript date to string.

How to convert JavaScript date to string?

To convert JavaScript date to string, we use some date methods.

For instance, we write

const temp = new Date();
const dateStr =
  temp.getFullYear().toString().padStart(2, "0") +
  (1 + temp.getMonth()).toString().padStart(2, "0") +
  temp.getDate().toString().padStart(2, "0") +
  temp.getHours().toString().padStart(2, "0") +
  temp.getMinutes().toString().padStart(2, "0") +
  temp.getSeconds().toString().padStart(2, "0");

to get the 4 digit year with getFullYear.

We get the month with 0 for January to 11 for December with getMonth.

We get the date with getDate.

We get the hours with getHours.

We get the minutes with getMinutes.

And we get the seconds with getSeconds.

We convert each number to a string with toString.

Then we call padStart to prepend '0' to each string until it has length 2.

And then we concatenate them all together.

Conclusion

To convert JavaScript date to string, we use some date methods.

Categories
JavaScript Answers

How to set focus to iframe contents with JavaScript?

Sometimes, we want to set focus to iframe contents with JavaScript.

In this article, we’ll look at how to set focus to iframe contents with JavaScript.

How to set focus to iframe contents with JavaScript?

To set focus to iframe contents with JavaScript, we call the iframe’s body’s focus method.

For instance, we write

document
  .getElementsByName("iframe_name")[0]
  .contentWindow.document.body.focus();

to get the iframe with getElementsByName.

Then we get its body element with contentWindow.document.body.

And then we call focus to focus on it.

Conclusion

To set focus to iframe contents with JavaScript, we call the iframe’s body’s focus method.

Categories
JavaScript Answers

How to add labels or legends for all chart types in Chart.js and JavaScript?

Sometimes, we want to add labels or legends for all chart types in Chart.js and JavaScript.

In this article, we’ll look at how to add labels or legends for all chart types in Chart.js and JavaScript.

How to add labels or legends for all chart types in Chart.js and JavaScript?

To add labels or legends for all chart types in Chart.js and JavaScript, we use the chart’s generateLegend method.

For instance, we write

<div>
  <canvas id="chartDiv" height="400" width="600"></canvas>
  <div id="legendDiv"></div>
</div>

to add a canvas for the chart and a div for the legend.

Then we write

const data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
      label: "Speed 1",
      fillColor: "rgba(220,220,220,0.2)",
      strokeColor: "rgba(220,220,220,1)",
      pointColor: "rgba(220,220,220,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(220,220,220,1)",
      data: [65, 59, 56, 24, 56, 55, 6],
    },
    {
      label: "Speed 2",
      fillColor: "rgba(151,187,205,0.2)",
      strokeColor: "rgba(151,187,205,1)",
      pointColor: "rgba(151,187,205,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(151,187,205,1)",
      data: [28, 1, 54, 76, 24, 12, 6],
    },
  ],
};

const myLineChart = new Chart(
  document.getElementById("chartDiv").getContext("2d")
).Line(data);
document.getElementById("legendDiv").innerHTML = myLineChart.generateLegend();

to create a chart with the canvas and the Chart constructor.

We call Line to create a line chart with the data.

Then we call myLineChart.generateLegend to return an HTML string with the legend’s content and set it as the innerHTML value of the div to show it.

Conclusion

To add labels or legends for all chart types in Chart.js and JavaScript, we use the chart’s generateLegend method.

Categories
JavaScript Answers

How to customize the colors of individual series with HighCharts and JavaScript?

Sometimes, we want to customize the colors of individual series with HighCharts and JavaScript.

In this article, we’ll look at how to customize the colors of individual series with HighCharts and JavaScript.

How to customize the colors of individual series with HighCharts and JavaScript?

To customize the colors of individual series with HighCharts and JavaScript, we set the color property of each object in the series array property.

For instance, we write

const chart = new Highcharts.Chart({
  chart: {
    renderTo: "container",
  },
  xAxis: {
    type: "datetime",
  },

  series: [
    {
      name: "John",
      color: "#0066FF",
      dashStyle: "ShortDash",
      data: [
        [Date.UTC(2022, 0, 1), 29.9],
        [Date.UTC(2022, 2, 1), 71.5],
        [Date.UTC(2022, 3, 1), 106.4],
      ],
    },
    {
      name: "Jane",
      color: "#FF0000",
      data: [
        [Date.UTC(2022, 0, 1), 60.9],
        [Date.UTC(2022, 1, 1), 40.5],
        [Date.UTC(2022, 2, 1), 90.0],
        [Date.UTC(2022, 3, 1), 80.4],
      ],
    },
  ],
});

to create a chart with the Highcharts.Chart constructor.

We call it with an object that has the series property set to an array with the series data.

We set the series color by setting the color property.

Conclusion

To customize the colors of individual series with HighCharts and JavaScript, we set the color property of each object in the series array property.

Categories
JavaScript Answers

How to reset textbox value in JavaScript?

To reset textbox value in JavaScript, we set the value property to an empty string.

For instance, we write

document.getElementById("searchField").value = "";

to select the input with getElementById.

Then we set its value property to an empty string.

Conclusion

To reset textbox value in JavaScript, we set the value property to an empty string.