Stupid enough question. There is a feedback form, in the form there are several inputs. For each input you need to apply a function on onblur, in order to catch the values ​​entered by the user. Tell me how to do this without referring to each input by id? Such code refers only to the first input.

document.getElementsByTagName('input')[0].onblur = function() { console.log(this); } 
  • jQuery not used? - Byulent

3 answers 3

 var arr = document.querySelectorAll('input'); for (var i = 0; i < arr.length; i++) { arr[i].onblur = function() { console.log(this); }; } 
     var inputs = document.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) { inputs[i].addEventListener('blur', function() { console.log(this); }, true); } 

      On jQuery

       $('input').blur(function(){ console.log(this); });