Categories
JavaScript Answers

How to add div element to body or document in JavaScript?

Sometimes, we want to add div element to body or document in JavaScript.

In this article, we’ll look at how to add div element to body or document in JavaScript.

How to add div element to body or document in JavaScript?

To add div element to body or document in JavaScript, we can use the createElement and appendChild methods.

For instance, we write

const elem = document.createElement("div");
elem.style.cssText = `
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0.3;
  z-index: 100;
  background: #000;
`;
document.body.appendChild(elem);

to create a div with createElement.

Then we set the CSS styles of the div to

`
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0.3;
  z-index: 100;
  background: #000;
`

Then we append the div as a child of the body element woth

document.body.appendChild(elem);

Conclusion

To add div element to body or document in JavaScript, we can use the createElement and appendChild methods.

Categories
JavaScript Answers

How to delete cookie by name with JavaScript?

Sometimes, we want to delete cookie by name with JavaScript.

In this article, we’ll look at how to delete cookie by name with JavaScript.

How to delete cookie by name with JavaScript?

To delete cookie by name with JavaScript, we set document.cookie to a string that has the name of the cookie followed by the expiry date.

For instance, we write

const deleteCookie = (name) => {
  document.cookie = name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
};

to add 'Expires=Thu, 01 Jan 1970 00:00:01 GMT;' after name to set the expiry date of the cookie with name to the past to remove it in the deleteCookie function.

Conclusion

To delete cookie by name with JavaScript, we set document.cookie to a string that has the name of the cookie followed by the expiry date.

Categories
JavaScript Answers

How to fix Cannot read property ‘addEventListener’ of null with JavaScript?

Sometimes, we want to fix Cannot read property ‘addEventListener’ of null with JavaScript.

In this article, we’ll look at how to fix Cannot read property ‘addEventListener’ of null with JavaScript.

How to fix Cannot read property ‘addEventListener’ of null with JavaScript?

To fix Cannot read property ‘addEventListener’ of null with JavaScript, we should make sure we’re calling addEveentListener on an element.

For instance, we write

const el = document.getElementById("overlayBtn");

if (el) {
  el.addEventListener("click", onClick, false);
}

to select an element with ID overlayBtn with getElementById.

Then we use an if statement to check if el is truthy.

If it is, then we call addEventListener on it since it’s an element if it’s not null.

Conclusion

To fix Cannot read property ‘addEventListener’ of null with JavaScript, we should make sure we’re calling addEveentListener on an element.

Categories
JavaScript Answers

How to remove elements by class name with JavaScript?

Sometimes, we want to remove elements by class name with JavaScript.

In this article, we’ll look at how to remove elements by class name with JavaScript.

How to remove elements by class name with JavaScript?

To remove elements by class name with JavaScript, we can use the remove method.

For instance, we write

document.querySelectorAll(".user-info").forEach((el) => el.remove());

to select all the elements with class user-info with querySelectorAll.

Then we call forEach on the node list with a callback to call el.remove to remove each element from the DOM.

Conclusion

To remove elements by class name with JavaScript, we can use the remove method.

Categories
JavaScript Answers

How to check whether dynamically attached event listener exists or not with JavaScript?

Sometimes, we want to check whether dynamically attached event listener exists or not with JavaScript.

In this article, we’ll look at how to check whether dynamically attached event listener exists or not with JavaScript.

How to check whether dynamically attached event listener exists or not with JavaScript?

To check whether dynamically attached event listener exists or not with JavaScript, we can get and set the element’s listener attribute.

For instance, we write

export const attachEvent = (
  element: Element,
  eventName: string,
  callback: () => void
) => {
  if (element && eventName && element.getAttribute("listener") !== "true") {
    element.setAttribute("listener", "true");
    element.addEventListener(eventName, () => {
      callback();
    });
  }
};

to get the listener attribute with element.getAttribute("listener").

If it’s set to 'true', then the listener is attached.

Otherwise, we attach it with addEventListener and call setAttribute to set the listener attribute to true.

Then we use it like

attachEvent(domElement, "click", listenerFunc)

by calling it with the element, event name, and event handler function.

Conclusion

To check whether dynamically attached event listener exists or not with JavaScript, we can get and set the element’s listener attribute.