[whatwg] Scripting and WF2 repetition model?
Ian Hickson
ian at hixie.ch
Wed Mar 14 12:09:52 PDT 2007
On Wed, 14 Mar 2007, Dave Raggett wrote:
>
> What I haven't yet worked out is how to address the fields in each row
> from the onforminput event handler set on the form element.
>
> [...]
> alert("rows = " + form.row[0].item.value);
That should be "form["row0.item"].value".
The [foo] bit in the name="" attributes get expanded, so that the names
are "row0.item", "row1.item", etc; the actual repetition blocks don't have
the square brackets. Look at the post-repetition DOM to see what I mean.
(The square-bracket dereference notation is required because there's a "."
in the name of the field.)
Anyway, you just want:
<p>Total price: <output onforminput="value = total()">0</output><?p>
...with:
<script>
function total() {
var rows = document.getElementsByTagName('tbody')[0].rows;
var result = 0;
for (var i = 0; i < rows.length; ++i) {
var prefix = 'row' + rows[i].repetitionIndex;
result += rows[i].getElementsByName(prefix + 'quantity')[0].value *
rows[i].getElementsByName(prefix + 'unitprice')[0].value;
}
return result;
}
</script>
By the way, the help at whatwg.org mailing list is probably more appropriate
for this thread. (That's our authoring help mailing list.)
HTH,
--
Ian Hickson U+1047E )\._.,--....,'``. fL
http://ln.hixie.ch/ U+263A /, _.. \ _\ ;`._ ,.
Things that are impossible just take longer. `._.-(,_..'--(,_..'`-.;.'
More information about the whatwg
mailing list