Current index of an array of form elements?

N

Norman

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

Norman escribió:
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?

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 :)
 
N

Norman

Norman escribió:


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?

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
--

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
 
N

Norman

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
 
T

Thomas 'PointedEars' Lahn

Norman said:
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;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top