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
Angular Answers

How to generate command model in Angular Project with Angular-CLI?

Sometimes, we want to generate command model in Angular Project with Angular-CLI.

In this article, we’ll look at how to generate command model in Angular Project with Angular-CLI.

How to generate command model in Angular Project with Angular-CLI?

To generate command model in Angular Project with Angular-CLI, we generate a class.

For instance, we run

ng generate class hero --type=model

to generate the hero model class.

We set the type option to model to generate a model class.

Then we get the hero.model.ts file as a result.

Conclusion

To generate command model in Angular Project with Angular-CLI, we generate a class.

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.

Categories
React Answers

How to only allow numbers in a number input in React?

Sometimes, we want to only allow numbers in a number input in React.

In this article, we’ll look at how to only allow numbers in a number input in React.

How to only allow numbers in a number input in React?

To only allow numbers in a number input in React, we can check for the key that’s pressed.

For instance, we write

<input
  onKeyPress={(event) => {
    if (!/[0-9]/.test(event.key)) {
      event.preventDefault();
    }
  }}
/>

to set the onKeyPress prop to a function that checks the key that’s pressed with event.key.

If it’s not 0 to 9, then we call preventDefault to stop the character from being appending to the input value.

Conclusion

To only allow numbers in a number input in React, we can check for the key that’s pressed.