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

137
Views
Get type of value entered

I have a small piece of code where I need to get the typeof value entered. Right now, i am getting only value entered in the box, but not the typeof. Can anyone help me here?

<p id="res">Value</p><input type="text" id="fname" name="fname"><br><br>

<script>
    document.getElementById('fname').addEventListener('input', function(){
        document.getElementById('res').textContent= this.value;
    });
</script>

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

You need to use typeof keyword, and if you want to test number vs string then you need to do a check type by using isNaN and Number

I changed your function to arrow function

document.getElementById('fname').addEventListener('input', e => {
  const value = e.currentTarget.value;
  const checkType = isNaN(Number(value)) ? value : Number(value)
  document.getElementById('res').textContent = typeof checkType
})
<p id="res">Value</p><input type="text" id="fname" name="fname"><br><br>

about 4 years ago · Santiago Gelvez Report

0

So typeof is not going to help you here. because the input is type="text" everthing typed into it will be casted to a string. exp: some types a 4, will be given to the js as "4". Im assuming you dont care about the type itself and just really want to know if they typed in a number. if thats the case, use the isNaN Function which will return false if a value can be casted to a number

isNaN("4") //false isNaN("hello") //true

PS: NaN stands for not a number

about 4 years ago · Santiago Gelvez 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!