Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!