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

125
Views
JS innerHTML when uses a function types not what it is supposed to
<body>
    <div class="container">
        <input type="number" id="first">
        <input type="number" id="second">
        <button id="butId">Максимум</button>
        <p id="out">
            123
        </p>
    </div>
    
    <script>        
    function min(a, b) {
        console.log('1');
        if (a>b) return b
        else return a;
    }
    butId.addEventListener('click', function(e){
       out.innerHTML=min(first,second);  
    })
    </script>
</body>

it supposed to show in <p id='out'> the minimal number of the two typed in. it shows

[object HTMLInputElement]

instead of a number and i do not see any errors

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

0

You are attempting to find the minimum value of each element, as opposed to the number inside it. You instead want to use

out.textContent = Math.min(first.valueAsNumber,second.valueAsNumber)

Here, we are getting the valueAsNumber of each input box and performing our min() calculation on it. This takes the text inside the input box and converts it to a number. You may also notice the Math.min, JS has a min function built in, so you do not need to define your own function for it. Additionally, it's good practice to use textContent to prevent XSS injection.

Math.min() - MDN

valueAsNumber attribute - MDN

about 4 years ago · Juan Pablo Isaza Report

0

You need to get the actual value entered into each <input> element, not the element itself.

butId.addEventListener('click', function(e) {
    out.innerHTML = min(+first.value, +second.value);
});

Note: The + before the variables converts them to ints.

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!