Categories
JavaScript Answers

How to get the current YouTube video time with JavaScript?

Sometimes, we want to get the current YouTube video time with JavaScript.

In this article, we’ll look at how to get the current YouTube video time with JavaScript.

How to get the current YouTube video time with JavaScript?

To get the current YouTube video time with JavaScript, we use the getCurrentTime method.

For instance, we write

const ytPlayer = document.getElementById("movie_player");
const time = ytplayer.getCurrentTime();

to select the YouTube player element with getElementById.

Then we call getCurrentTime to get the current time of the video.

Conclusion

To get the current YouTube video time with JavaScript, we use the getCurrentTime method.

Categories
JavaScript Answers

How to change img src and change image color on file input change with JavaScript?

Sometimes, we want to change img src and change image color on file input change with JavaScript.

In this article, we’ll look at how to change img src and change image color on file input change with JavaScript.

How to change img src and change image color on file input change with JavaScript?

To change img src and change image color on file input change with JavaScript, we can listen to the change event.

For instance, we write

const fileToRead = document.getElementById("yourFile");

fileToRead.addEventListener(
  "change",
  (event) => {
    const [file] = fileToRead.files;
    if (fileToRead.files.length) {
      console.log("Filename: ", file.name);
      console.log("Type: ", file.type);
      console.log("Size: ", file.size);
    }
  },
  false
);

to select the file input with getElementById.

Then we call addEventListener to listen for the change event on the fileToRead input.

In the event handler, we get the file from the files property.

And we get the file name, type and size from the selected file.

Conclusion

To change img src and change image color on file input change with JavaScript, we can listen to the change event.

Categories
JavaScript Answers

How to get current YouTube video time with JavaScript?

Sometimes, we want to get current YouTube video time with JavaScript

In this article, we’ll look at how to get current YouTube video time with JavaScript

How to get current YouTube video time with JavaScript?

To get current YouTube video time with JavaScript, we use the getCurrentTime method.

For instance, we write

const ytPlayer = document.getElementById("movie_player");
ytPlayer.getCurrentTime();

to get the YouTube player with getElementById.

Then we get the current time with the getCurrentTime method.

Conclusion

To get current YouTube video time with JavaScript, we use the getCurrentTime method.

Categories
JavaScript Answers

How to set style on body with JavaScript?

Sometimes, we want to set style on body with JavaScript.

In this article, we’ll look at how to set style on body with JavaScript.

How to set style on body with JavaScript?

To set style on body with JavaScript, we set the style properties.

For instance, we write

document.body.style.display = "block";

to set the display property style to 'block' on the body.

document.body is the body element.

Conclusion

To set style on body with JavaScript, we set the style properties.

Categories
JavaScript Answers

How to prevent anchor behavior with JavaScript?

Sometimes, we want to prevent anchor behavior with JavaScript.

In this article, we’ll look at how to prevent anchor behavior with JavaScript.

How to prevent anchor behavior with JavaScript?

To prevent anchor behavior with JavaScript, we return false in the event handler.

For instance, we write

<a href="no-script.html" id="myLink">link</a>

to add a link.

Then we write

document.getElementById("myLink").onclick = () => {
  // ...
  return false;
};

to get the link with getElementById.

And then we set its onclick property to a function that’s called when the link is clicked.

We return false to stop the default behavior.

Conclusion

To prevent anchor behavior with JavaScript, we return false in the event handler.