If given 1 element in an array of textboxes... find which element number it is

A

\A_Michigan_User\

I call this Javascript function with "GetEleNum(this)"
passing it 1 of the elements of a text-box array.

Is there a QUICK way to get the "element #"? I'm currently using this
SLOW code.
(It works... but I need something better/faster.)

function GetEleNum(c)
{
// Given 1 element in this textBox array... what is the array element #?

var i;

for(i=0; i<form1.txtBox.length; i++)
if(form1.txtBox == c) return i;

return 0; // Not found
}

Thanks
 
D

Darko

I call this Javascript function with "GetEleNum(this)"
passing it 1 of the elements of a text-box array.

Is there a QUICK way to get the "element #"? I'm currently using this
SLOW code.
(It works... but I need something better/faster.)

function GetEleNum(c)
{
// Given 1 element in this textBox array... what is the array element #?

var i;

for(i=0; i<form1.txtBox.length; i++)
if(form1.txtBox == c) return i;

return 0; // Not found

}

Thanks


Try organizing your question into something understandable, PLEASE!
This way, I don't know what you're asking, what you're trying to
accomplish, anything.

Suggestion nr. 1: don't use 'u' instead of 'you', '1' instead of
'one', '#' instead of 'number' etc.
Suggestion nr. 2: explain what specific terms mean, e.g. what your
txtBox's -are-, what 'element #' means
(I suppose it's an ordinal number in an array, but -
what- array, or have you maybe meant
element's ID, etc.
Suggestion nr. 3: the way you're doing it right now - DOESN'T work.
What if the element is on the first place
(index 0)? You'll understand it the same like it
wasn't found, right?

Regards
 
R

RobG

I call this Javascript function with "GetEleNum(this)"
passing it 1 of the elements of a text-box array.

Is there a QUICK way to get the "element #"? I'm currently using this
SLOW code.
(It works... but I need something better/faster.)

function GetEleNum(c)
{
// Given 1 element in this textBox array... what is the array element #?
var i;
for(i=0; i<form1.txtBox.length; i++)
if(form1.txtBox == c) return i;

return 0; // Not found
}


I don't know why you think that is slow. The following function can
find a particular item in an collection of 1,000 elements in less than
15ms on a very average PC.

function getIndex(el){
var els = document.getElementsByTagName('input');
var i = els.length;
while(i--){
if (els == el) {
return i;
}
}
}

If you want to do it faster, include the index in the name or ID of
the element, something like "input_23". Then you just need to parse
the name or ID to extract the index, say:

return el.name.split('_')[1];

and you're done.
 
T

Thomas 'PointedEars' Lahn

A_Michigan_User said:
// Given 1 element in this textBox array... what is the array element #?

BTW: It is not an array; it is a collection.


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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top