mercredi 2 novembre 2016

(Javascript) global "If" statement when checkbox is checked?

A little question here, I'm trying to execute a huge calculation that will determine something randomly within a value range. I want the user to determine this range himself, and let him choose wether he wants to add a maximum value or a minimum value. Basically, everything works for now, but I'm trying to put an "if" statement at the beginning to check if the checkboxes are checked or not in order to prompt the proper questions. The problem is, the way I'm writing it right now makes it impossible to read the program: it simply does nothing. The error message displayed is:
Uncaught TypeError: Cannot read property 'checked' of null(…).
I tried a few things, and when I set the values myself, it works, so the problem is really the "if" statement. Here are my global vars and the part that doesnt work:

    var maxdesiredvalue, mindesiredvalue;

                    if (document.getElementById("min").checked == false) {
                        mindesiredvalue = 0.00
                    }
                    else {
                    mindesiredvalue = window.prompt("Valeur minimale désirée:"); 
                    mindesiredvalue = parseInt(mindesiredvalue)}

                    if (document.getElementById("max").checked == false) {
                        maxdesiredvalue = 9999999.99
                    }
                    else {
                    maxdesiredvalue = window.prompt("Valeur maximale désirée:");
                    maxdesiredvalue = parseInt(maxdesiredvalue)}

        function RandWeap() {
             Huge function here determining a value and repeating until it 
             is between mindesiredvalue and maxdesiredvalue
        }

The checkboxes are written like this:

<body>
    <form>
    <i> Press F5 to clear or to get 1 random weapon </i> <br> <br>
    Min price: <input type="checkbox" id="min"> <br>
    Max price: <input type="checkbox" id="max"> <br> <br>
    <button type="button" onclick="RandWeap()">Add a random weapon to list</button>
    </form>
</body>

Any idea of how to get it working?

Thank you!




Aucun commentaire:

Enregistrer un commentaire