/    Sign up×
Community /Pin to ProfileBookmark

Returning the index-number of an arrayed field

Hi there,

I have a form with some arrayed text fields in it, a table with a fixed number of columns and a variable number of rows. All the fields in the first row have the same name as the fields in the second row and in the third row etc. Something like this:
Row 1: fieldA[1] fieldB[1] fieldC[1]
Row 2: fieldA[2] fieldB[2] fieldC[2]
.
.
Row n: fieldA[n] fieldB[n] fieldC[n]

(where [1], [2] and [n] are the array index numbers).

When data has been entered in fieldA and the field loses focus, the form is submitted (by an onfocusout-event) and refreshed, and fieldB is filled in automatically (the value is determined by a database-lookup).

My problem/question is this:
When the form is refreshed, I would like to put focus in the right row in my form table. I plan on using an onload-event for this, but I need to know which row had the focus before the refresh.

My onfocusout-event (on fieldA) looks like this:
onFocusout=customSubmit(this)

where customSubmit is defined like this:
function customSubmit(elem) {
document.getElementById(“currentField”).value = elem.name;
[form name].submit();
}

The idea is that the name of the “triggering” field is stored in the parameter currentField just before the form is submitted.

This ALMOST does what I want – except that the name stored in my currentField parameter is always “fieldA”, not “fieldA[1]” or “fieldA[2]” (i.e. the array index number is missing). Is there any way to include the index number in the name stored in the currentField parameter?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliMay 23.2007 —  Is there any way to include the index number in the name stored in the currentField parameter?[/quote]I am assuming that when you say the fields all have the same name, you mean that literally and that you are not naming them with square brackets, i.e.: "fieldA[1]".

The fact that you're getting a name like 'fieldA' returned suggest that is the case.

This is intended to generate a 0-based subscript, but is totally untested:
function customSubmit(elem)
{
var group=elem.form.elements[elem.name];

for(var i=0; i<group.length && group[i]!=elem; i++)
;

document.getElementById("currentField").value = elem.name+'['+i+']';

elem.form.submit();
}
Copy linkTweet thisAlerts:
@AndreasDKauthorMay 23.2007 — 
Originally posted by [B]Logic Ali[/B]:

I am assuming that when you say the fields all have the same name, you mean that literally and that you are not naming them with square brackets, i.e.: "fieldA[1]".

The fact that you're getting a name like 'fieldA' returned suggest that is the case.[/QUOTE]


Your assumption is correct ?

And, thank you for your code suggestion, it seems to work as intended ?
×

Success!

Help @AndreasDK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...