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

381
Views
In flexbox, submit button is not working properly. How to fix it?

let formBtn = document.querySelector('#formBtn');

formBtn.addEventListener('click', function check_condition(){

let formName = document.querySelector('#formname').value;

if(formName == " "){
  alert("Kindly enter your name");
}

});
.left-form{
    width: 60%;
    display: flex;
    flex-direction: column;
   gap: 16px;
   transition: all 0.6 ease;
}
.left-form h4{
    font-size: 26px;
}

.right-adrs{
    width: 40%;
    background-color: orange;
}

.form-inputs{
    padding: 12px 16px;
    color: lightgray;
    width: 90%;
}
#formBtn{
    width: 20%;
    padding: 16px 16px;
    background-color: #355E3B;
    color: white;
    border:#355E3B;
    cursor: pointer;
    
}

textarea{
    height: 100px;
    width: 90%;
}
<div class="left-form">
        <h4>Get in touch</h4>
            <input type="text" class="form-inputs" id="formname" placeholder=" Name" />
            <input type="email" class="form-inputs formemail" placeholder=" Email"/>
            <input type="tel" class="form-inputs formtel" placeholder=" Phone number"/>
            <textarea placeholder=" Message"></textarea>
            <button type="submit"  id="formBtn">Send Message</button>
    </div>
I want to make a form in which the values can't go blank. If somebody clicks on button without entering the values, the alert box pops up. to design it, I have used flex property. but now the JavaScript is not working. Can somebody explain why it is so?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's not related to the flexbox, that's happened because you check if the formname value is equal to empty space, but it should be an empty string formName.trim() === "" or formName.length === 0.

let formBtn = document.querySelector('#formBtn');

formBtn.addEventListener('click', function check_condition(){
  let formName = document.querySelector('#formname').value;
  
  if(formName.trim() === ""){
    alert("Kindly enter your name");
  }
});
.left-form{
    width: 60%;
    display: flex;
    flex-direction: column;
   gap: 16px;
   transition: all 0.6 ease;
}
.left-form h4{
    font-size: 26px;
}

.right-adrs{
    width: 40%;
    background-color: orange;
}

.form-inputs{
    padding: 12px 16px;
    color: lightgray;
    width: 90%;
}
#formBtn{
    width: 20%;
    padding: 16px 16px;
    background-color: #355E3B;
    color: white;
    border:#355E3B;
    cursor: pointer;
    
}

textarea{
    height: 100px;
    width: 90%;
}
<div class="left-form">
        <h4>Get in touch</h4>
            <input type="text" class="form-inputs" id="formname" placeholder=" Name" />
            <input type="email" class="form-inputs formemail" placeholder=" Email"/>
            <input type="tel" class="form-inputs formtel" placeholder=" Phone number"/>
            <textarea placeholder=" Message"></textarea>
            <button type="submit"  id="formBtn">Send Message</button>
    </div>

almost 4 years ago · Santiago Trujillo 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!