Fix to remove whitespace while typing into a input text field

//Fix to remove whitespace while typing into a input text field

Fix to remove whitespace while typing into a input text field

This has been haunting me for a while now.

While one might think that is a easy thing to do, just listen to the keydown og keyup event, og even using jquery’s keypress event and adding a

preventdefault on the event, or returning false – then youre done. 🙂

But hey – NOT working on the  mobile chrome browser.

So it seems that anothe technique must be used.

After a long trial and error period, i came across a new event called input

And voila:

var your_element = document.getElementById('your_element');
your_element.addEventListener('textInput', function(e) {

  if (e.data.charCodeAt(0) == 32) {
     e.preventDefault();
     return false;
  }
  return true;

});

/Renzo

By | 2018-01-06T01:52:20+00:00 September 8th, 2016|Coding|Comments Off on Fix to remove whitespace while typing into a input text field