473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Current index of an array of form elements?

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
Jun 27 '08 #1
4 2629
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
--
Jun 27 '08 #2
On 8 maio, 12:02, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.comwrote:
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
Jun 27 '08 #3
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
Jun 27 '08 #4
Norman wrote:
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
--
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, <f8*******************@news.demon.co.uk>
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
7
by: Magnus Warker | last post by:
Hi, I want to traverse an (associative) array, starting from its current position, until a certain element is reached. Then, in certain cases, I want to be able to reset the current position of...
4
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
8
by: Joe Rattz | last post by:
Ok, I can't believe what I am seeing. I am sure I do this other places with no problems, but I sure can't here. I have some code that is indexing into the ItemArray in a DataSet's DataRow. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.