Forum Moderators: open

Message Too Old, No Replies

Javascript array / form "name" question

Really, it's gotta be simple...

         

Br3nn4n

7:25 pm on Apr 1, 2010 (gmt 0)

10+ Year Member



I'm working on a simple calculator. See code:

 <form name="income" action="#">
<table style="width: 500px;">
<tr style="background-color: #000;"><td colspan="3">Income</td></tr>
<tr><td style="background: gray;">&nbsp;</td><td>Lots</td><td style="background: lightyellow;"><input type="text" name="numbers[]" /></td></tr>
<tr><td style="background: gray;">&nbsp;</td><td>Pips Per Day</td><td style="background: lightyellow;"><input type="text" name="numbers[]" /></td></tr>
<tr><td style="background: gray;">&nbsp;</td><td>Days Per Week</td><td style="background: lightyellow;"><input type="text" name="numbers[]" onblur="calculate('income', document.forms('income').numbers[]);" /></td></tr>
<tr><td style="background: gray;">&nbsp;</td><td>Pips Per Month</td><td style="background: ;"></td></tr>
<tr><td style="background: gray;">&nbsp;</td><td style="background: limegreen;">$ Profit Per Month</td><td style="background: limegreen;"></td></tr>
</table>
</form>


So as you see, there's just 3 input boxes, and when the 3rd is filled in it triggers a JS function. I want to just use a simple array, called "numbers[]", for the "name" part of each <input>. It seems that I've used this concept before, but I can't remember.

I'm getting ridiculous errors, like "unexpected token at ]". I was trying to just bunch the 3 into an array, and pass that ARRAY to the calculate() function.

It just says it's undefined if I alert() any of the elements.

Also tried - giving each one a number in the array like so:

<input type="text" name="numbers[0]" />

Any tips are welcomed :) Maybe I'm doing it all wrong?

Fotiman

7:40 pm on Apr 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this:

onblur="calculate('income', document.forms['income']['numbers[]']);"

rainborick

12:03 am on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I often feel like I missed the last 10 years of the evolution of JavaScript, so is it actually OK now to use the same 'name' attribute in your HTML and expect JavaScript parse out the fact that this is an array name and it's value should be automatically incremented?

astupidname

1:58 am on Apr 3, 2010 (gmt 0)

10+ Year Member



so is it actually OK now to use the same 'name' attribute in your HTML and expect JavaScript parse out the fact that this is an array name and it's value should be automatically incremented?

I think you may be confused by the array syntax of the naming of the form elements?
See: Creating Arrays in html forms [php.net]

rainborick

3:16 pm on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK. It's a PHP thing. I feel less stupid now... a little. Thanks.