Categories
JavaScript Answers

How to listen to the play event on a HTML video element with JavaScript?

Sometimes, we want to listen to the play event on a HTML video element with JavaScript.

In this article, we’ll look at how to listen to the play event on a HTML video element with JavaScript.

How to listen to the play event on a HTML video element with JavaScript?

To listen to the play event on a HTML video element with JavaScript, can set onplay property of the video element to the event listener function.

For instance, we write:

<video src='https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4' controls></video>

to add a video.

Then we write:

const video = document.querySelector('video')
video.onplay = () => {
  console.log('playing')
}

to select the video element with querySelector.

Then we set video.onplay to a function that logs 'playing'.

Therefore, if the video is playing, 'playing' is logged.

Conclusion

To listen to the play event on a HTML video element with JavaScript, can set onplay property of the video element to the event listener function.

Categories
JavaScript Answers

How to render empty space in React and JavaScript?

Sometimes, we want to render empty space in React and JavaScript.

In this article, we’ll look at how to render empty space in React and JavaScript.

How to render empty space in React and JavaScript?

To render empty space in React and JavaScript, we can put the character code for spaces into the code.

For instance, we write:

import React from "react";

export default function App() {
  return <>foo{"\u00a0\u00a0"}bar</>;
}

to add "\u00a0\u00a0" to add 2 spaces between foo and bar.

Conclusion

To render empty space in React and JavaScript, we can put the character code for spaces into the code.

Categories
JavaScript Answers

How to check if an element has been loaded on a page before running a script with JavaScript?

Sometimes, we want to check if an element has been loaded on a page before running a script with JavaScript.

In this article, we’ll look at how to check if an element has been loaded on a page before running a script with JavaScript.

How to check if an element has been loaded on a page before running a script with JavaScript?

To check if an element has been loaded on a page before running a script with JavaScript, we can use the mutation observer.

For instance, we write:

const observer = new MutationObserver((mutationRecords) => {
  console.log("change detected");
});
observer.observe(document.body, {
  childList: true
})

setTimeout(() => {
  const div = document.createElement('div')
  document.body.appendChild(div)
}, 100)

to create a MutationObserver instance.

Then we call observer.observer with the element we want to observe and an object that sets the items we want to observe,.

We set childList to true to watch for DOM item changes.

Next, we create a div and call document.body.appendChild with div to append a div to the body element.

And when we did that, we should see 'change detected' logged.

Conclusion

To check if an element has been loaded on a page before running a script with JavaScript, we can use the mutation observer.

Categories
JavaScript Answers

How to stop anchor element click from making page jump to top of page with JavaScript?

Sometimes, we want to stop anchor element click from making page jump to top of page with JavaScript.

In this article, we’ll look at how to stop anchor element click from making page jump to top of page with JavaScript.

How to stop anchor element click from making page jump to top of page with JavaScript?

To stop anchor element click from making page jump to top of page with JavaScript, we call preventDefault in the anchor element’s click handler.

For instance, we write:

<a href='#'>click me</a>

to add the anchor element.

Then we write:

const a = document.querySelector('a')
a.onclick = (e) => {
  e.preventDefault()
}

to select the anchor element with querySelectorAll.

Then we set a.onclick to a function that calls e.preventDefault to stop the page from moving up when we click it.

Conclusion

To stop anchor element click from making page jump to top of page with JavaScript, we call preventDefault in the anchor element’s click handler.

Categories
JavaScript Answers

How to attach event listener to a radio button with JavaScript?

Sometimes, we want to attach event listener to a radio button with JavaScript.

In this article, we’ll look at how to attach event listener to a radio button with JavaScript.

How to attach event listener to a radio button with JavaScript?

To attach event listener to a radio button with JavaScript, we can loop through each radio button and set its onclick property.

For instance, we write:

<form>
  <input type="radio" name="myradio" value="A" />
  <input type="radio" name="myradio" value="B" />
  <input type="radio" name="myradio" value="C" />
  <input type="radio" name="myradio" value="D" />
</form>

to add a form with radio buttons.

Then we write:

const radios = document.querySelectorAll('input')
for (const radio of radios) {
  radio.onclick = (e) => {
    console.log(e.target.value);
  }
}

to select the radio buttons with querySelectorAll.

Then we loop through them with a for-of loop.

In it, we set radio.onclick to a function that logs the value attribute of the radio button.

Conclusion

To attach event listener to a radio button with JavaScript, we can loop through each radio button and set its onclick property.