Categories
JavaScript Answers

How to attach event to dynamic elements in JavaScript?

Sometimes, we want to attach event to dynamic elements in JavaScript.

In this article, we’ll look at how to attach event to dynamic elements in JavaScript.

How to attach event to dynamic elements in JavaScript?

To attach event to dynamic elements in JavaScript, we can listen for events on document.

For instance, we write

document.addEventListener("click", (e) => {
  if (e.target && e.target.id === "brnPrepend") {
    //...
  }
});

to call document.addEventListener to listen to click events on the whole document.

In the event listener callback, we check if the ID of the element that triggered the click is brnPrepend.

If it is, then we do something.

We do the element’s ID check with e.target.id === "brnPrepend".

e.target is the actual element that triggered the event.

Conclusion

To attach event to dynamic elements in JavaScript, we can listen for events on document.

Categories
JavaScript Answers

How to get the device width in JavaScript?

Sometimes, we want to get the device width in JavaScript.

In this article, we’ll look at how to get the device width in JavaScript.

How to get the device width in JavaScript?

To get the device width in JavaScript, we use the window.innerWidth or screen.width properties.

For instance, we write

const width = window.innerWidth > 0 ? window.innerWidth : screen.width;

to check if window.innerWidth is bigger than 0.

If it is, then we return that as the width.

Otherwise, we return screen.width.

window.innerWidth is available in the desktop.

And screen.width is available on mobile devices.

Conclusion

To get the device width in JavaScript, we use the window.innerWidth or screen.width properties.

Categories
Vue Answers

How to reload route in Vue Router?

Sometimes, we want to reload route in Vue Router.

In this article, we’ll look at how to reload route in Vue Router.

How to reload route in Vue Router?

To reload route in Vue Router, we call this.$router.go in our component.

We call it with no argument to do the same thing as windwo.history.go called with no argument, which reloads the page.

Conclusion

To reload route in Vue Router, we call this.$router.go in our component.

Categories
JavaScript Answers

How to monitor the memory usage of Node.js?

Sometimes, we want to monitor the memory usage of Node.js.

In this article, we’ll look at how to monitor the memory usage of Node.js.

How to monitor the memory usage of Node.js?

To monitor the memory usage of Node.js, we use the process.memoryUsage method.

For instance, we write

const formatMemoryUsage = (data) =>
  `${Math.round((data / 1024 / 1024) * 100) / 100} MB`;

const memoryData = process.memoryUsage();

const memoryUsage = {
  rss: formatMemoryUsage(memoryData.rss),
  heapTotal: formatMemoryUsage(memoryData.heapTotal),
  heapUsed: formatMemoryUsage(memoryData.heapUsed),
  external: formatMemoryUsage(memoryData.external),
};

console.log(memoryUsage);

to get an object that various properties.

rss is the Resident Set Size, which is the total memory allocated for the process execution.

heapTotal is the total size of the allocated heap.

heapUsed is the amount of memory used.

external is the V8 external memory amount.

All values are in bytes.

Conclusion

To monitor the memory usage of Node.js, we use the process.memoryUsage method.

Categories
JavaScript Answers

How to show alternate image if source image is not found with JavaScript?

Sometimes, we want to show alternate image if source image is not found with JavaScript.

In this article, we’ll look at how to show alternate image if source image is not found with JavaScript.

How to show alternate image if source image is not found with JavaScript?

To show alternate image if source image is not found with JavaScript, we can listen to the error event of the img element.

For instance, we write

const img = document.getElementById("myImage");
img.onerror = () => {
  img.src = "alternate.png";
};

to get the img element with getElementById.

Then we set img.onerror to a function that runs when the error event is triggered by the img element.

The event will be triggered if the image fails to load.

In the function, we set the src property of the img element to an alternative image URL.

Conclusion

To show alternate image if source image is not found with JavaScript, we can listen to the error event of the img element.