0
tin.onchange = addNewValue;

      function addNewValue(e){
          c1 = e.target.value;
        console.log(c1);
           return c1;
       }

            
       tin2.onchange = addNewValue2;
       function addNewValue2(f){
          c2 = f.target.value;
          return c2;
       }
       line2 = c1 + c2;
        ;
        console.log(line2);

I am trying to have the variable which is filled with the user by a dynamic appearing form return to a string which I can add to the original string. But the link between them doesn't work.

What am I missing?

  • `line2 = c1 + c2;` will run before your onchange functions execute. – Nick Parsons Dec 18 '20 at 12:01
  • @NickParsons how do I return the values to that variable? – Ahmad Fathy Dec 18 '20 at 12:31
  • The two links above have detailed answers on how you'd go about doing that. But, in short, your `return c1` and `return c2` are not doing anything and aren't useful. Setting a global variable can be useful (eg: `c1 = e.target.value`) _if_ you access the variable after it has been set. Consider running your addition inside of both addNewValue functions, or creating a button which the user can click to add the two buttons. If you run the addition inside the onchange functions, you'd need to do something about c1 and c2 being undefined initially before the user enters a value for both fields – Nick Parsons Dec 18 '20 at 12:36

0 Answers0