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

233
Views
How to compare two values from input text?

what's wrong with my code? The true alert never comes out even if I put a right answer,0414. Only false alert comes out.

var answer = String(document.getElementById('Properdate').value);
var rightanswer = '0414';
 
<input id="Properdate" type="text">

<input type="submit" value="submit" onclick="if(answer === rightanswer){
 alert('good'); } else { alert('Try again');}" >
 

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

0

Try using a button tag since you are not submitting a form. Give it an ID so you can reference it from your JS file.

HTML

<input id="properdate" type="text">
<button id="submit" type="button">Submit</button>

From you JS file or tag. The "let" keyword lets you work with the variable in scope, you can declare it once and use it in different blocks and with different values.

Select your "submit" button element and use "addEventListener()" instead of the onclick attribute. This does not overwrite existing event handlers as the "onclick" does. From there, make sure you are using the correct event, in this case "click" and create a function to do whatever you need to. Use your if clauses and try to be as clean as possible. Keep working on that, pal!

JS

<script>
    let answer = document.getElementById('properdate');
    let rightanswer = '0414';
    let submit = document.getElementById('submit');

    submit.addEventListener('click', function(){
        if(answer.value === rightanswer) {
            alert('Good');
        } else {
            alert('Try again');
        }
    });
</script>
about 4 years ago · Juan Pablo Isaza Report

0

I think it is becouse you are retrieving the value only when the page loads, when the event called it uses the value stored in answer which is the one it saved when page loaded.

<input id="Properdate" type="text">
<script>
var rightanswer= '0414';
</script>
<input type="submit" value="submit" onclick="
var answer = String(document.getElementById('Properdate').value);
if(answer === rightanswer){
 alert('good');
}
else{
alert('Try again');}"
 >

Should work with this

about 4 years ago · Juan Pablo Isaza Report

0

It's not a good idea to put the code as an onclick on the button! I made an example which I hope will be useful to you.

Example:

document.getElementById('sbmt').addEventListener('click', submitFunc);

function submitFunc() {
    var answer = document.getElementById('Properdate').value;
    var rightanswer = '0414';

    if (answer === rightanswer) {
        alert('good');
    }
    else {
        alert('Try again');
    }
}
<input id="Properdate" type="text">
<input id="sbmt" type="submit" value="submit">

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!