vendredi 15 février 2019

adding values to an array through an if statement in javascript

I get values and ids from a form input using onkeyup(). If the value is between 0 and 4 I want to add it to an array. However it seems my function adds values to the array regardless if they are between 0 and 4 eventhough I want it to add values to the array only when the value is between 0 and 4. What am I doing wrong?

These are the inputs:

<input type="number" class="form-control" id=0 onkeyup="a(this)" mix="0" max="4">
<input type="number" class="form-control" id=1 onkeyup="a(this)" mix="0" max="4">
.
.

This is my function:

function a(c){

    var dps = [];
    var id = c.id;
    var valueStr = c.value;
    var value = parseInt(valueStr)
    if (0 <= value <= 4) {
        dps[id]=value;
        console.log("id: ",id);
        console.log("value: ", value);
        console.log("length of array: ",dps.length);
        console.log("type of value: ", typeof value);
    }
 }

I have recreated it on Jsfiddle here: https://jsfiddle.net/89az7u4n/

Aucun commentaire:

Enregistrer un commentaire