简体   繁体   中英

I have this error: Cannot read property 'value' of null, I dont understand why

I'm getting error in this code, I am making a calculator that can add, subtract, multiply and divide, the values of the numbers the code reads them correctly, but when I try to do the operations I get the error, and I don't understand why the code doesn't read it, can someone tell me what I'm doing wrong? this is the code

function handleNumber(stNumber) {
    if(stNumber=="numero1")
    {
        Num1 = document.getElementById(stNumber).value*1;
    }else{
        Num2 = document.getElementById(stNumber).value*1;
    }
    return Num1, Num2;
}

function handleOperate() {
    var stOperation = document.getElementById(operaciones).value; 
    Execute(stOperation);
}

function Execute(stOperation) {
    var stResult = ""
    if(stOperation == "sumar"){
        stResult= sumar();
    } else if(stOperation =="restar"){
        stResult= restar();

    } else if(stOperation =="multiplicar"){
        stResult= multiplicar();

    } else if(stOperation =="dividir"){
        stResult= dividir();

    }

    document.getElementById("result").innerHTML = stResult;
}

this is the html

<div class="Container-operation">
            <span>Operación</span>
            <div class="Button-container"  id="operator">
                <select onchange="handleOperate()" class="Panel-container" id="operaciones">
                    <option value="sumar">Sumar</option>
                    <option value="restar">Restar</option>
                    <option value="multiplicar">División</option>
                    <option value="dividir">Multiplicación</option>
                  </select>
          </div>
          </div>
        </div>

        <div class="Equal">
          =
        </div>

        <div class="Result-container">
          <span id="result">Resultado</span>
        </div>

You're passing a variable operaciones to getElementById() . If you don't set a value for this variable it will not work. You probably want to pass a string 'operaciones' instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM