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

Discussion in 'Javascript' started by \A_Michigan_User\, Nov 15, 2007.

  1. 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
    \A_Michigan_User\, Nov 15, 2007
    #1
    1. Advertising

  2. \A_Michigan_User\

    Darko Guest

    Re: If given 1 element in an array of textboxes... find which elementnumber it is

    On Nov 15, 8:59 pm, "\"A_Michigan_User\""
    <> 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 == 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
    Darko, Nov 15, 2007
    #2
    1. Advertising

  3. \A_Michigan_User\

    RobG Guest

    Re: If given 1 element in an array of textboxes... find which elementnumber it is

    On Nov 16, 5:59 am, "\"A_Michigan_User\""
    <> 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 == 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.


    --
    Rob
    RobG, Nov 16, 2007
    #3
  4. Re: If given 1 element in an array of textboxes... find which elementnumber it is

    "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
    Thomas 'PointedEars' Lahn, Nov 16, 2007
    #4
  5. Re: If given 1 element in an array of textboxes... find which elementnumber it is

    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
    Thomas 'PointedEars' Lahn, Nov 16, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    2
    Views:
    491
  2. Matt
    Replies:
    1
    Views:
    388
    Martin Honnen
    Jun 5, 2004
  3. anne001
    Replies:
    4
    Views:
    170
    Robert Klemme
    Aug 11, 2008
  4. Replies:
    3
    Views:
    93
    DJ Stunks
    Jul 18, 2006
  5. stalin
    Replies:
    3
    Views:
    227
    Dr J R Stockton
    Jan 30, 2008
Loading...

Share This Page