Search select list?

Discussion in 'Javascript' started by jojowebdev@gmail.com, Jun 22, 2006.

  1. Guest

    I have been TRYING for a few days to get a *working* search of a select
    list (about 800 options in list). I have not found one that works for
    IE6.

    Here is what I *last* tried. I would appreciate any help to get me
    through this. Thank you.

    function searchList(textInput) {
    var carrier_list = document.carrRequestForm.carrierList;
    for (i = 0; i < carrier_list.length; i++) {
    if (carrier_list.indexOf(textInput.value) == 0) {
    carrier_list.selectedIndex = i;
    break;
    }
    }
    }


    Then in body..

    <form action="process_Carrier_Request_Form.asp?..etc.." method="post"
    name="carrRequestForm">

    Quick Carrier Search: <br />
    <input type="text" id="search" onkeyup="searchList(this)"/><br />

    <select size="19" id="carrierList">
    <%=carrier_name%> <!-- list of 800 carriers -->
    </select>
    <br />
    <input class="frmButton" type="submit" value="Select Carrier and Close
    Window"/><br />

    </form>
     
    , Jun 22, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > I have been TRYING for a few days to get a *working* search of a select
    > list (about 800 options in list). I have not found one that works for
    > IE6.


    I think a number have been posted here.


    > Here is what I *last* tried. I would appreciate any help to get me
    > through this. Thank you.
    >
    > function searchList(textInput) {
    > var carrier_list = document.carrRequestForm.carrierList;
    > for (i = 0; i < carrier_list.length; i++) {


    You should keep i local, letting counters slip into the global space can
    cause serious problems.

    If you have 800 options, it will be more efficient to get the length
    once and store it rather than looking it up (potentially) 800 times:

    for (var i=0, len=carrier_list.length; i<len; i++) {


    > if (carrier_list.indexOf(textInput.value) == 0) {


    carrier_list will return an option element. It doesn't have an
    indexOf() method, so you should get an error here.

    However, the *value* of the option is a string, and you can use
    indexOf() with that, so:

    if ( carrier_list.value.indexOf(textInput.value) == 0) {


    will return true if textInput matches the start of the value (or all of it).

    > carrier_list.selectedIndex = i;
    > break;


    I don't like 'break', just return.


    <script type="text/javascript">

    function searchList(el)
    {
    var txt = el.form.elements['text_0'].value;
    var opts = el.form.elements['sel_A'].options;
    var t;

    for (var i=0, len=opts.length; i<len; ++i){
    t = opts;
    if ( t.value.indexOf(txt) == 0){
    t.selected = true;
    return;
    }
    }
    }

    </script>

    <form action="">
    <div>
    <select name="sel_A">

    <!-- For testing to create 100 options -->
    <script type="text/javascript">
    var t = '';
    for (var i=0; i<100; i++){
    t += '<option value="' + i + '">' + i;
    }
    document.write(t);
    </script>

    </select>
    <input type="text" id="text_0">
    <input type="button" value="Select..."
    onclick="searchList(this);">
    </div>
    </form>



    --
    Rob
     
    RobG, Jun 22, 2006
    #2
    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. Abby Lee
    Replies:
    5
    Views:
    420
    Abby Lee
    Aug 2, 2004
  2. Pranjal Jain
    Replies:
    3
    Views:
    158
    saras
    Apr 10, 2008
  3. Dan Thomas
    Replies:
    0
    Views:
    280
    Dan Thomas
    Feb 2, 2011
  4. palmiere
    Replies:
    1
    Views:
    413
    Erwin Moller
    Feb 9, 2004
  5. carrajo

    Select List transfer to another List

    carrajo, Feb 16, 2006, in forum: Javascript
    Replies:
    5
    Views:
    313
Loading...

Share This Page