0

my return var doesn't want to return. What do i do wrong? i get this error in the console:

Uncaught ReferenceError: totaal1 is not defined .

var $ = function (id) {
    return document.getElementById(id);
}
$("totaalprijs").innerHTML = totaal1 + totaal2;
$("gerecht1").onchange = function () {
    var aantal = $("gerecht1").value;
    var prijs = 7.9;
    console.log(aantal);
    totaal1 = aantal * prijs;
    return totaal1;
}
$("gerecht2").onchange = function () {
    var aantal = $("gerecht2").value;
    var prijs = 9.9;
    console.log(aantal);
    totaal2 = aantal * prijs;
    return totaal2;
}
Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
user3071261
  • 376
  • 2
  • 10
  • 21

3 Answers3

4

On this line:

$("totaalprijs").innerHTML = totaal1 + totaal2;

...you're trying to use a variable, totaal1, that isn't declared anywhere. Hence the ReferenceError, if you try to read the value of a variable that isn't declared, it causes that error.

To fix it, declare the variable (and ideally assign it a meaningful value), or don't use it in that calculation. (And then do the same for totaal2, because it doesn't seem to be declared anywhere, either.)

Now, the fun thing here is that if the change event fires on gerecht1 and gerecht2, totaal1 and totaal2 will get implicitly created as global variables, via The Horror of Implicit Globals. But because those change handlers aren't attached until after the code causing the problem, they never get attached, because of the error.

FWIW, I think you probably wanted something like this:

var totaal1 = 0, totaal2 = 0;
var $ = function (id) {
    return document.getElementById(id);
};
var updateTotaalPrijs = function() {
  $("totaalprijs").innerHTML = totaal1 + totaal2;
};
updateTotaalPrijs();
$("gerecht1").onchange = function () {
    var aantal = parseFloat($("gerecht1").value); // Or possibly `parseInt(..., 10)`
    var prijs = 7.9;
    console.log(aantal);
    totaal1 = aantal * prijs;
    updateTotaalPrijs();
};
$("gerecht2").onchange = function () {
    var aantal = parseFloat($("gerecht2").value); // Or possibly `parseInt(..., 10)` 
    var prijs = 9.9;
    console.log(aantal);
    totaal2 = aantal * prijs;
    updateTotaalPrijs();
};

And also FWIW, I would suggest using function declarations most of the time, and wrapping everything to avoid creating globals:

(function() {
  var totaal1 = 0, totaal2 = 0;

  function $(id) {
      return document.getElementById(id);
  }

  function updateTotaalPrijs() {
    $("totaalprijs").innerHTML = totaal1 + totaal2;
  }

  updateTotaalPrijs();

  $("gerecht1").onchange = function () {
      var aantal = parseFloat($("gerecht1").value); // Or possibly `parseInt(..., 10)`
      var prijs = 7.9;
      console.log(aantal);
      totaal1 = aantal * prijs;
      updateTotaalPrijs();
  };
  $("gerecht2").onchange = function () {
      var aantal = parseFloat($("gerecht2").value); // Or possibly `parseInt(..., 10)` 
      var prijs = 9.9;
      console.log(aantal);
      totaal2 = aantal * prijs;
      updateTotaalPrijs();
  };
})();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

you have to declare totaal1 and totaal2 variable so try below code:

 var totaal1=0,totaal2=0;
 var $ = function (id) 
 {
     return document.getElementById(id);
 }
 $("totaalprijs").innerHTML = totaal1 + totaal2;
 $("gerecht1").onchange = function () 
 {
    var aantal = $("gerecht1").value;
    var prijs = 7.9;
    console.log(aantal);
    totaal1 = aantal * prijs;
    return totaal1;
  }
 $("gerecht2").onchange = function () 
 {
    var aantal = $("gerecht2").value;
    var prijs = 9.9;
    console.log(aantal);
    totaal2 = aantal * prijs;
    return totaal2;
 }
Vaibhav Parmar
  • 645
  • 4
  • 11
0

In line

$("totaalprijs").innerHTML = totaal1 + totaal2;

you're trying to access global variables totaal1 and totaal2, which are local to event handlers functions. The handler return value is purposed for internal DOM event processing and not working this way.

What you want is to update #totaalprijs, when gerecht1 and gerecht2 are updated, so you better to create common onchange handler, which will do all calculations

var $ = function(id) {
    return document.getElementById(id);
}

// Common change handler
function calc(e) {
   var aantal1 = $("gerecht1").value,
       aantal2 = $("gerecht2").value,
       prijs1 = 7.9,
       prijs2 = 9.9,
       total = aantal1 * prijs1 + aantal2 * prijs2;
   $("totaalprijs").innerHTML = total;
}
$("gerecht1").onchange = calc;
$("gerecht2").onchange = calc;
calc(); // Call on load, to calculate on initial values
oxfn
  • 6,590
  • 2
  • 26
  • 34