Skip to content

Instantly share code, notes, and snippets.

@kitsune7
Created December 14, 2018 07:59
Show Gist options
  • Select an option

  • Save kitsune7/e1997e02a7bea8f5501a443282370113 to your computer and use it in GitHub Desktop.

Select an option

Save kitsune7/e1997e02a7bea8f5501a443282370113 to your computer and use it in GitHub Desktop.
Utility functions for displaying errors in vanilla.js (Relies on having a hidden attribute set on element at start state)
/*
* Displays in error in the first matched CSS selector given.
* This relies on having a hidden attribute set on element at start state.
* @param error: string The error message
* @param selector: string A CSS selector that matches the element displaying the error
*/
function displayError (error, selector) {
const errorElement = document.querySelector(selector)
errorElement.innerHTML = error
errorElement.removeAttribute('hidden')
}
/*
* Removes the error message from the element and hides it again
* @param selector: string A CSS selector that matches the element displaying the error
*/
function clearError (selector) {
const errorElement = document.querySelector(selector)
errorElement.innerHTML = ''
errorElement.setAttribute('hidden', 'hidden')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment