W3C home > Mailing lists > Public > whatwg@whatwg.org > March 2007

[whatwg] Scripting and WF2 repetition model?

From: Ian Hickson <ian@hixie.ch>
Date: Wed, 14 Mar 2007 19:09:52 +0000 (UTC)
Message-ID: <Pine.LNX.4.62.0703141855240.15496@dhalsim.dreamhost.com>
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.   `._.-(,_..'--(,_..'`-.;.'
Received on Wednesday, 14 March 2007 12:09:52 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:58:53 UTC