0
    function calcul() {
        salaire = window.parseInt(document.myForm.txtSalaire.value);
        temps = window.parseInt(document.myForm.txtTemps.value);
        for (i = 0; i < monnaie.length; i++) {
            profit = monnaie[i] - (salaire/DIVISEUR) * temps;
            profit = profit.toFixed(3);
            document.myForm.txtMonnaie[i].value = monnaie[i] + "$";
            document.myForm.txtProfit[i].value = profit;
        }
    }
... <input type="text" name="txtMonnaie0"/>

I want to run through all my inputs to set new values to 'name="txtMonnaie[]"' and txtProfit[] using i as the parameter.

myForm.txtMonnaie is undefined.

2
  • 1
    How do you define monnaie? Commented Feb 6, 2013 at 16:55
  • 1
    You can't access DOM properties before you define them in your markup (i.e., put that script to the bottom of your body element) Commented Feb 6, 2013 at 16:59

1 Answer 1

1

Change:

 document.myForm.txtMonnaie[i].value = monnaie[i] + "$";
 document.myForm.txtProfit[i].value = profit;

To

 document.myForm["txtMonnaie" + i].value = monnaie[i] + "$";
 document.myForm["txtProfit" +i].value = profit;

If you have multiple elements with names like txtProfit0, txtProfit1, txtProfit2, ... browser will not create an array with name txtProfit in document.myForm. There you will find just a list of properties like document.myForm.txtProfit0, document.myForm.txtProfit1 ... and you can access them using index, just like it is shown above

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.