0

I have made a simple function and it is supposed to run on window.onload. But instead, I get that it is undefined. Can anyone help me understand why I get this error?

The form id is kalkulator and the name of the inputs is liter and enheter

The function I have written is

window.onload = function () {
    form.kalkulator.focus();
};

I have also written this

form.kalkulator = function () {
    if (form.enheter.value == "") {
        form.liter.value = "";
    } else{
        convertLiter();
    };
}

function convertLiter() {
    console.log(form.liter.value / person.volum() * person.sukker_g())
    form.enheter.value = form.liter.value / person.volum();

}
function convertEnheter() {
    console.log(form.enheter.value * person.sukker_g())
    form.liter.value = form.enheter.value * person.volum();
}
cchapman
  • 3,269
  • 10
  • 50
  • 68
user262669
  • 25
  • 1
  • 8

2 Answers2

0

This has nothing to do with window.onload.

You're invoking this...

form.kalkulator.focus();

but form.kalkulator is a function, it doesn't have a .focus() method. form.klalkulator.focus is undefined, and you're attempting to invoke it as a function, hence your error.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

form.kalkulator doesn't have any return so you can't call

  form.kalkulator.focus();

replace the function

form.kalkulator = function () {
    if (form.enheter.value == "") {
        form.liter.value = "";
    } else{
        convertLiter();
   };
return form;
}

or split the instructions;

form.kalkulator();
form.focus();
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30