vendredi 4 septembre 2020

Getting NaN when trying to use a number from an input field in an if statment

I am having a problem with part of my reflection. I am trying to get table cell to show how many miles are left until charging the car is needed.

I have an input cell where the user would type how many miles are left (50) and then a calculation happens (50 - (10% buffer, 25) = 25 and displays on the cell next to it.

I have gone through the questions on here I can find and tried the approaches others have tried but I really am at a dead end now.

The JSFiddle is here : https://jsfiddle.net/Coxy/gr5bcnsq/

The code is

var car_1_mtnc = document.getElementById('car-1-mtnc');
$(".confirm").click(function() {
var a =  parseInt(document.getElementById('car-1-cmra').value, 10);
var miles = a - 25;  

if ( a != Number ) {
    car_1_mtnc.textContent = "Please enter the miles left";
} else {
    car_1_mtnc.textContent = miles;
}
});

HTML

                                    <td><span id="car-1-cmra" class="answer" type="number"></span><input id="car-1-cmr" class="cmr" type="text" value="" placeholder="Enter Miles Left"></input></td>
                                    <td id="car-1-mtnc" class="mtnc"></td>

Any pointers would be really appreciated, thank you for your time.

Aucun commentaire:

Enregistrer un commentaire