How To Remove Extra White Space From The Start and End of All Form Inputs

Using the trim() method, we can select all form inputs with document.querySelectorAll(), then iterate over them and bind them to a change event listener.



If the value of any element is changed, the function expression executes and removes extra white space at the start and end of its value using the trim() method.

Here's the code for implementation

let formInputs = document.querySelectorAll('input, textarea');
formInputs.forEach(input = {
input.addEventListener('change', function(){
this.value = this.value.trim();
});
})

Simon Ugorji

16 Blog posts

Comments