Categories
JavaScript Answers

How to convert 24 hour time to 12 hour time with AM & PM using JavaScript?

Sometimes, we want to convert 24 hour time to 12 hour time with AM & PM using JavaScript.

In this article, we’ll look at how to convert 24 hour time to 12 hour time with AM & PM using JavaScript.

How to convert 24 hour time to 12 hour time with AM & PM using JavaScript?

To convert 24 hour time to 12 hour time with AM & PM using JavaScript, we use the toLocaleString method.

For instance, we write

const date = new Date("February 04, 2022 19:00:00");
const options = {
  hour: "numeric",
  minute: "numeric",
  hour12: true,
};
const timeString = date.toLocaleString("en-US", options);
console.log(timeString);

to call toLocaleString with the locale string and the options object.

In options, we set hour12 to true to return a string with 12 hour time and AM or PM.

hour and minute are set to numeric to return them as numbers.

Conclusion

To convert 24 hour time to 12 hour time with AM & PM using JavaScript, we use the toLocaleString method.

Categories
JavaScript Answers

How to change select option and trigger events with JavaScript?

Sometimes, we want to change select option and trigger events with JavaScript.

In this article, we’ll look at how to change select option and trigger events with JavaScript.

How to change select option and trigger events with JavaScript?

To change select option and trigger events with JavaScript, we trigger the event manually.

For instance, we write

<select id="sel">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3" selected>Three</option>
</select>
<input type="button" value="Change option to 2" />

to add a select and input element.

Then we write

const select = document.querySelector("#sel");
const input = document.querySelector('input[type="button"]');

select.addEventListener("change", () => {
  alert("changed");
});

input.addEventListener("click", () => {
  select.value = 2;
  select.dispatchEvent(new Event("change"));
});

to select both elements with querySelector.

And then we listen for the change event on the select element with select.addEventListener.

Then we add a click event listener to the input button to set the select’s value when it’s clicked and trigger the change event on it with dispatchEvent.

Conclusion

To change select option and trigger events with JavaScript, we trigger the event manually.

Categories
JavaScript Answers

How to add a grouped bar charts in chart.js and JavaScript?

Sometimes, we want to add a grouped bar charts in chart.js and JavaScript.

In this article, we’ll look at how to add a grouped bar charts in chart.js and JavaScript.

How to add a grouped bar charts in chart.js and JavaScript?

To add a grouped bar charts in chart.js and JavaScript, we add the data for each bar in the chart.

For instance, we write

const ctx = document.getElementById("myChart").getContext("2d");

const data = {
  labels: ["Chocolate", "Vanilla", "Strawberry"],
  datasets: [
    {
      label: "Blue",
      backgroundColor: "blue",
      data: [3, 7, 4],
    },
    {
      label: "Red",
      backgroundColor: "red",
      data: [4, 3, 5],
    },
    {
      label: "Green",
      backgroundColor: "green",
      data: [7, 2, 6],
    },
  ],
};

const myBarChart = new Chart(ctx, {
  type: "bar",
  data,
  options: {
    barValueSpacing: 20,
    scales: {
      yAxes: [
        {
          ticks: {
            min: 0,
          },
        },
      ],
    },
  },
});

to set data as the value of the data property in the object we call the Chart constructor with.

And we set the type to 'bar' so that the data will be rendered as a grouped bar chart.

Conclusion

To add a grouped bar charts in chart.js and JavaScript, we add the data for each bar in the chart.

Categories
JavaScript Answers

How to find all elements whose id begins with a common string with JavaScript?

Sometimes, we want to find all elements whose id begins with a common string with JavaScript.

In this article, we’ll look at how to find all elements whose id begins with a common string with JavaScript.

How to find all elements whose id begins with a common string with JavaScript?

To find all elements whose id begins with a common string with JavaScript, we call querySelectorAll.

For instance, we write

const dates = document.querySelectorAll('[id^="createdOnId"]');

to call querySelectorAll with '[id^="createdOnId"]' to return a node list with all elements that has ID starting with createdOnId.

Conclusion

To find all elements whose id begins with a common string with JavaScript, we call querySelectorAll.

Categories
JavaScript Answers

How to stop a requestAnimationFrame recursion or loop with JavaScript?

Sometimes, we want to stop a requestAnimationFrame recursion or loop with JavaScript.

In this article, we’ll look at how to stop a requestAnimationFrame recursion or loop with JavaScript.

How to stop a requestAnimationFrame recursion or loop with JavaScript?

To stop a requestAnimationFrame recursion or loop with JavaScript, we can add a flag to check when to stop calling requestAnimationFrame .

For instance, we write

let pause = false;
const loop = () => {
  //...
  if (pause) {
    return;
  }
  window.requestionAnimationFrame(loop);
};

loop();
pause = true;
loop();

to define the loop function that returns before calling requestionAnimationFrame if pause is true.

Then we call loop before and after we set pause to true.

Therefore, the first loop call will call requestionAnimationFrame and the 2nd one won’t.

Conclusion

To stop a requestAnimationFrame recursion or loop with JavaScript, we can add a flag to check when to stop calling requestAnimationFrame .