Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
    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.

share|improve this question
1  
How do you define monnaie? –  bfavaretto Feb 6 '13 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) –  Marcel Korpel Feb 6 '13 at 16:59

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.