Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
How to excludes spaces as a user input?

So I want to add items to my list by entering a value and clicking the button. I have managed to ignore empty user input but I can't figure out how to exclude spaces as user input? I can't use a conditional since the number of spaces a user enters is unpredictable.

<input id="userinput" type="text" placeholder="Enter items">
<button id="add">Click Me!</button>
<ul id="list">
    <li>pen</li>
    <li>pencil</li>
    <li>paper</li>
</ul>
<script>
    var input = document.getElementById("userinput");
    var button = document.getElementById("add");
    var ul = document.querySelector("ul");
    

    button.addEventListener("click", function(){
        if (input.value.length > 0) {
            var li = document.createElement("li")
            li.appendChild(document.createTextNode(input.value));
            ul.appendChild(li);
        }    
    } )

</script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To remove spaces at the beginning or/and end of a string, use str.trim(), to remove all spaces, use a regex with str.replace:

str.replace(/( )/g, "");

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda