javascript – How to remove blanks and capitalize at the same time

Question:

I get toUpperCase and also blank spaces, but how do I make them both, that as soon as I write in the text field, it converts me to uppercase and removes spaces?

 document.getElementById('campoNombre').onkeyup = sanear; function sanear(){ let contenido = document.getElementById('campoNombre').value; let contenidoSaneado = contenido.toUpperCase(); let contenidoSinEspacios = contenido.replace(' ', ''); document.getElementById('campoNombre').value = contenidoSaneado; document.getElementById('campoNombre').value = contenidoSinEspacios; }

Answer:

you were calling the onkeyup event wrong, I leave you the example of how it should be.

document.getElementById("campoNombre").addEventListener('keyup', sanear);


function sanear(e) {
  let contenido = e.target.value;
  e.target.value = contenido.toUpperCase().replace(" ", "");
}
<input id="campoNombre" type="text">

The methods can be concatenated and will be executed sequentially.

Scroll to Top