Code Notes

Useful JavaScript functions & snippets

Automatically remove an event listener after it has executed

el.addEventListener('click', console.log, {
  once: true,
})

The magical handleEvent function

// Get a reference to the <button>
const btn = document.querySelector('button')

// Define object with `handleEvent` function
const myObject = {
  handleEvent: (event) => {
    alert(event.type)
  },
}

// Listen for 'click' events on the <button> and handle them with `myObject`... WHAT?!?!
btn.addEventListener('click', myObject)

More info

Remove query param

export const removeSearchParam = (name: string): void => {
  const url = new URL(location.href)
  url.searchParams.delete(name)
  history.replaceState(null, '', url)
}