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

100
Views
alert box not working properly in JavaScript
<script>
    function fun(){
          let i=document.getElementById("input")
          if(i==="sam")
            {
               alert("welcome SAM")
            }
          else
            {
               alert("welcome user")
            }
        }
      </script>
     
        <input id="input"/>
        <button onclick="fun()">click</click>

if user type input as 'sam' it should be follow the if block but every time it execute else part .how to change the condition that when user type 'sam' in textbox then only it follow if block or else execute else part

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

getElementById method returns html element, if you want to get text inside input you should use document.getElementById("input")?.value

about 4 years ago · Juan Pablo Isaza Report

0

  1. Make sure your input is correct.

  2. Instead of writing "if(i===sam)", write "if(i=="sam").

  3. Other than that, I think there's a problem with your input method. Javascript doesn't directly get the input from the box. For further information, check this link: https://www.tutorialspoint.com/How-to-take-user-input-using-HTML-forms

  4. If you want to do it the easier way, just put "document.getElementById("input").value"

If this answer helped you, please mark it as an answer

about 4 years ago · Juan Pablo Isaza Report

0

Just add .value ahead of document.getElementById("input")

Just like that:

let i=document.getElementById("input").value

Basically, you're not passing the text to the variable i that is the reason else is executing

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!