Categories
JavaScript Answers

How to prettify JSON data in text area input with JavaScript?

Sometimes, we want to prettify JSON data in text area input with JavaScript.

In this article, we’ll look at how to prettify JSON data in text area input with JavaScript.

How to prettify JSON data in text area input with JavaScript?

To prettify JSON data in text area input with JavaScript, we use the JSON.stringify method.

For instance, we write

<textarea id="myTextArea" cols="50" rows="10"></textarea>
<button>Pretty Print</button>

to add a text area and a button.

Then we write

const button = document.querySelector("button");

button.onclick = () => {
  const ugly = document.getElementById("myTextArea").value;
  const obj = JSON.parse(ugly);
  const pretty = JSON.stringify(obj, undefined, 4);
  document.getElementById("myTextArea").value = pretty;
};

to select the button with querySelector.

And then we set button.onclick to a function that formats the JSON by calling JSON.parse to parse the ugly JSON string.

Next we call JSON.stringify with obj and 4 to format the string with 4 spaces for indentation.

And then we assign the formatted pretty JSON string back to the text area.

Conclusion

To prettify JSON data in text area input with JavaScript, we use the JSON.stringify method.

Categories
JavaScript Answers

How to round up or down a moment.js to the nearest minute with JavaScript?

Sometimes, we want to round up or down a moment.js to the nearest minute with JavaScript.

In this article, we’ll look at how to round up or down a moment.js to the nearest minute with JavaScript.

How to round up or down a moment.js to the nearest minute with JavaScript?

To round up or down a moment.js to the nearest minute with JavaScript, we use the startOf method.

For instance, we write

const roundDown = moment("2022-02-17 12:59:59").startOf("hour");
roundDown.format("HH:mm:SS");

to round up to the nearest hour by calling startOf.

Then we write

const roundUp = (momentObj, roundBy) => {
  return momentObj.add(1, roundBy).startOf(roundBy);
};

const a = moment("2022-02-17 12:00:00");
const roundedUp = roundUp(a, "minute").format("HH:mm:SS");

to define the roundUp function.

In it, we call add with 1 and roundBy to add 1 roundBy unit to the moment object.

Then we call startOf with roundBy to round to the nearest roundBy unit.

Next we call roundUp and format to round up and format the time returned.

Conclusion

To round up or down a moment.js to the nearest minute with JavaScript, we use the roundTo method.

Categories
JavaScript Answers

How to convert a YouTube video URL to embed code with JavaScript?

Sometimes, we want to convert a YouTube video URL to embed code with JavaScript.

In this article, we’ll look at how to convert a YouTube video URL to embed code with JavaScript.

How to convert a YouTube video URL to embed code with JavaScript?

To convert a YouTube video URL to embed code with JavaScript, we manipulate the URL to convert it to the video embed URL.

For instance, we write

const str = "https://www.youtube.com/watch?v=1adfD9";
const res = str.split("=");
const embeddedUrl = "https://www.youtube.com/embed/" + res[1];
document.getElementById("demo").src = res;

to get the embeddedUrl by getting the video ID with

const res = str.split("=");

and then use res[1].

Then we get the embeddedUrl with

const embeddedUrl = "https://www.youtube.com/embed/" + res[1];

Then we put it in an iframe element by setting its src property to the video embed URL.

Conclusion

To convert a YouTube video URL to embed code with JavaScript, we manipulate the URL to convert it to the video embed URL.

Categories
JavaScript Answers

How to convert milliseconds to time in JavaScript?

Sometimes, we want to convert milliseconds to time in JavaScript.

in this article, we’ll look at how to convert milliseconds to time in JavaScript.

How to convert milliseconds to time in JavaScript?

To convert milliseconds to time in JavaScript, we convert it to a date.

For instance, we write

const millisecondsToTime = (milli) => {
  const milliseconds = milli % 1000;
  const seconds = Math.floor((milli / 1000) % 60);
  const minutes = Math.floor((milli / (60 * 1000)) % 60);

  return minutes + ":" + seconds + "." + milliseconds;
};

to define the millisecondsToTime function.

In it, we get the milliseconds by using the % operrator.

Then we get the seconds by converting milli to seconds with (milli / 1000).

And then we use % to get the remainder when dividing that by 60.

Likewise, we get minutes by converting milli to minutes with (milli / (60 * 1000) and doing the same thing afterwards.

Finally, we concatenate the minutes, seconds, and milliseconds together.

Conclusion

To convert milliseconds to time in JavaScript, we convert it to a date.

Categories
JavaScript Answers

How to add click event listener to elements with the same class with JavaScript?

Sometimes, we want to add click event listener to elements with the same class with JavaScript.

In this article, we’ll look at how to add click event listener to elements with the same class with JavaScript.

How to add click event listener to elements with the same class with JavaScript?

To add click event listener to elements with the same class with JavaScript, we can loop through each element and call addEventListener on them.

For instance, we write

const deleteLinks = document.querySelectorAll(".delete");

Array.from(deleteLinks).forEach((link) => {
  link.addEventListener("click", (event) => {
    if (!confirm(`sure u want to delete ${event.target.title}`)) {
      event.preventDefault();
    }
  });
});

to select all the elements with class delete with `querySelectorAll“.

Then we convert deleteLinks to an array with Array.from.

And then we call forEach with a function that calls link.addEventListener to add a click listener to each element.

Conclusion

To add click event listener to elements with the same class with JavaScript, we can loop through each element and call addEventListener on them.