Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > If given 1 element in an array of textboxes... find which element number it is

Reply
Thread Tools

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

 
 
\A_Michigan_User\
Guest
Posts: n/a
 
      11-15-2007

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[i] == c) return i;

return 0; // Not found
}

Thanks



 
Reply With Quote
 
 
 
 
Darko
Guest
Posts: n/a
 
      11-15-2007
On Nov 15, 8:59 pm, "\"A_Michigan_User\""
<(E-Mail Removed)> wrote:
> 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[i] == 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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-16-2007
On Nov 16, 5:59 am, "\"A_Michigan_User\""
<(E-Mail Removed)> wrote:
> 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[i] == 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[i] == 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.


--
Rob
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-16-2007
"A_Michigan_User" wrote:
> // Given 1 element in this textBox array... what is the array element #?


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


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-16-2007
Thomas 'PointedEars' Lahn wrote:
> "A_Michigan_User" wrote:
>> // Given 1 element in this textBox array... what is the array element #?

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


Ignore that; in this case it is a node list.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how find no of 100 ,50,20,10,5,2,1 in a given number stalin Javascript 3 01-30-2008 12:49 PM
Days in a given date range for a given month......... Lord0 Java 1 04-19-2006 04:54 PM
generate all possible strings of given length given a set of characters chiara C Programming 6 10-06-2005 01:43 AM
get the number of children of a given element Matt XML 1 06-05-2004 03:25 PM
get the number of children of a given element Matt Java 2 06-05-2004 03:35 AM



Advertisments