Search code examples
javascriptonblur

Change backgroundcolour of input after onblur automatically across page


I would like an input field to have the background colour changed after onblur. Automatically for all fields on every page. So I am hoping for a script in the header that will automatically affect all input fields.

Can this be done?

Thanks!


Solution

  • window.onload = function(){
      Array.prototype.slice.call(document.getElementsByTagName('input')).forEach(function(element){
        element.addEventListener('blur',function(){
            //anything here. Notice that both this and element refer to each input element.
        })
      });
    }
    

    document.querySelectorAll or any function returning a NodeList object can be used.