In this function, the program does not want to execute my condition if. I stupidly tried to reverse the "if" in "else if", and "else if" to "if" but, of course, that doesn't change anything.

I want when the input value is <12000, the textual content of "amount6" to be changed to "unqualified!


<p class="minimum2">(Points total minimum groupe conseillers + groupe 
animateurs = 12000 )</p>
<div class="blocklabel5">
<label for="points amount" class="label6">Entrez points groupe 
<input type="number" class="amountEnter6" id="amount6"> 
<div class="blocklabel6">
<label for="points amount" class="label7">Entrez points groupe 
<input type="number" class="amountEnter7" id="amount7">
<input type="submit" value="Commissions pilote qualifié" id="submit7" 
class="submitEnter7" onclick="calcAmount6()">
<p id="marginAmount6" class="enterMargin6">0€</p>

JAVASCRIPT (calcAmount6)

function calcAmount6(){
var userAmount5 = document.getElementById("amount6").value;
var userAmount6 = document.getElementById("amount7").value; 
var oneLevel1 = 12000;
if (userAmount5 + userAmount6 < oneLevel1) {
document.getElementById("marginAmount6").textContent = "Non-qualifié!";}
else if (userAmount5 + userAmount6 >= oneLevel1) {
document.getElementById("marginAmount6").textContent = 
Math.round(userAmount5 * 13 / 100) + (userAmount6 * 5 / 100) + "€";}

3 Answers

MikNiller On

You must convert your strings to numbers

var userAmount5 = Number(document.getElementById("amount6").value);
var userAmount6 = Number(document.getElementById("amount7").value); 

Assuming your input is something like

var userAmount5 = document.getElementById("amount6").value; // 10
var userAmount6 = document.getElementById("amount7").value; // 20

then you get this result

userAmount5 + userAmount6 === '1020' // equals true

and not 30 as you may expect.

but if you convert your strings to numbers first you get the desired result

Number(userAmount5) + Number(userAmount6) === 30  // equals true
Vasil Dininski On

document.getElementById("id").value would return the value as a string, so you might need to cast it to a number: Number(document.getElementById("id").value)

Gabriel Romero On

Try with:

var userAmount5 = parseInt(document.getElementById("amount6").value);
var userAmount6 = parseInt(document.getElementById("amount7").value);