Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Current index of an array of form elements?

0 views
Skip to first unread message

Norman

unread,
May 8, 2008, 10:38:25 AM5/8/08
to
I am trying to find a way to implement something like:

<FORM name='form1'><TABLE>
server scripting loop (php), generates X (a variable number) lines
with the 3 fields {
<TR><TD><input type='text' name='kminicial[]'></TD>
<TD><input type='text' name='kmfinal[]'
onBlur='document.form1.kmrodados[currentindex].value=document.form1.kmfinal[currentindex].value-
document.form1.kminicial[currentindex].value'></TD>
<TD><input type='text' name='kmrodados[]'></TD></TR>
}
</TABLE></FORM>

Just a simple onBlur math with the first two fields of each line,
putting the result on the third. The problem is the "currentindex"
value, I wasnt able to find how to get this value anywhere, is it
possible to implement this way?

Thanks in advance,
Norman

"Álvaro G. Vicario"

unread,
May 8, 2008, 11:02:42 AM5/8/08
to
Norman escribió:

If you use PHP, why don't you do an extra step?

<?php

$rowid = 0;

?>

...

<input type=" name="kmfinal[]"
onblur="updateResultByRowId(<? echo $rowid++; ?>)">


Otherwise, I can only think of this:

<input type=" name="kmfinal[]"
onblur="updateResultByRightField(this)">


function updateResultByRightField(rightField){
var currentForm = rightField.form;

/*
* And then write code to find:
* var leftField = ......
* var resultField
*
* ... either going up from rightField or going
* down from currentForm...
*/
}


One more idea is to asign events to the row but I still prefer option 1 :)


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

Norman

unread,
May 8, 2008, 2:19:35 PM5/8/08
to
On 8 maio, 12:02, "Álvaro G. Vicario"
> --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain

> -- Mi sitio sobre programación web:http://bits.demogracia.com
> -- Mi web de humor al baño María:http://www.demogracia.com
> --

Thanks Alvaro, I tried your 1st approach:

<SCRIPT LANGUAGE=\"JavaScript\">
function calcula(indice) {
document.formulario.kmrodados[indice].value =
document.formulario.kmfinal[indice]-
document.formulario.kminicial[indice];
return true;
}
</SCRIPT>

and later, initialize the $indiceatual=0 on php, and inside the loop:

<TD><input type='text' name='kminicial[]'>

<TD><input type='text' name='kmfinal[]' onBlur='calcula(" .
$indiceatual++ . ");'>


<TD><input type='text' name='kmrodados[]'>

but this generates an javascript error stating:

this.document.formulario.kmrodados has no properties

on the 1st line of the calcula() function. The form name is declared
as 'formulario'. Can you see what Im doing wrong here?

Thank in advance,
Norman

Norman

unread,
May 8, 2008, 10:09:22 PM5/8/08
to
Solved. Hope this helps anyone: It was just a matter of referencing
the input fields using the elements object in the javascript function:

document.formulario.elements['kmrodados[]']
[indice].value=document.formulario.elements['kmfinal[]'][indice].value-
document.formulario.elements['kminicial[]'][indice].value;

Norman

Thomas 'PointedEars' Lahn

unread,
May 9, 2008, 10:12:13 AM5/9/08
to

Should be

var es = document.forms['formulario'].elements;
...
es['kmrodados[]'][indice].value =
es['kmfinal[]'][indice].value - es['kminicial[]'][indice].value;

instead. Probably you don't need document.forms['formulario'] and the
associated form name anyway, because you can pass the form object reference
from an included form control with `this.form', and from the `form' element
with `this'.

Also note that the `-' operation performs only a simple conversion (from
string) to number, which may lead to unexpected results e.g. with "0x42",
"077", or "078". You may want to use parseInt(..., 10) or parseFloat(...)
instead.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

0 new messages