Calculation with Inputs in JavaScript

Posted on

Question :

function IBUTG() {
    $tt = $('[name=tempo_trabalho]');
    $vt = $('[name=valor_ibutg_trabalho]');
    $td = $('[name=tempo_descanso]');
    $vd = $('[name=valor_ibutg_descanso]');

    $ibutg = (($tt * $vt) + ($td * $vd)) / 60;

    $('[name=ibutg_calculado]').val($ibutg);
}

I’m getting NaN while performing this function, what can it be? The values of the inputs are in the format 99.9

    

Answer :

The .val() was missing at the end

function IBUTG() {
    $tt = parseFloat($('[name=tempo_trabalho]').val());
    $vt = parseFloat($('[name=valor_ibutg_trabalho]').val());
    $td = parseFloat($('[name=tempo_descanso]').val());
    $vd = parseFloat($('[name=valor_ibutg_descanso]').val());

    $ibutg = (($tt * $vt) + ($td * $vd)) / 60;

    $('[name=ibutg_calculado]').val($ibutg);
}

    

Leave a Reply

Your email address will not be published. Required fields are marked *