Results 1 to 7 of 7

Thread: finding data in a ComboBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Posts
    150

    finding data in a ComboBox

    Hi all

    Let say i have a comboBox with 1000 of numbers

    Ex:1234,8574,6987,3254,6874..................

    By clicking on the combo instead of finding the number in the drop down i want to be able to click on a number like 6

    it will take you to first number that starts with 6

    after you press 9 so it will take you to the number starting with 69 and so on.......

    Is there a javascript code that can do that?

    Thanks

  2. #2
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    so you want put the text '6' on your page somewhere. then click it and be taken to the first element of your combo box that starts with 6 ???

  3. #3
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    try this !
    Code:
    <html>
    <head>
    </head>
    <body bgcolor="#FFFFFF" text="#000000">
    <script>
    function jumpToValue(first_char)
    {
    	the_form = document.getElementById('form1');
    	the_select = the_form.numbers;
    	found_pos = false;
    	
    	for(i = 0; i < the_select.options.length; i++)
    	{	
    		option_value = the_select.options[i].value;
    		if (option_value.indexOf(first_char) == 0 && found_pos == false)
    		{
    			the_select.options[i].selected = true;
    			found_pos = true;//found the first one so lets stop it from going any further
    				
    		}
    	}
    }
    </script>
    <form name="form1" method="post" action="">
      <select name="numbers" multiple size="3">
        <option value="12">12</option>
        <option value="23">23</option>
        <option value="34">34</option>
        <option value="45">45</option>
        <option value="56">56</option>
        <option value="67">67</option>
        <option value="78">78</option>
        <option value="89">89</option>
        <option value="91">91</option>
      </select>
    </form>
    <a href="javascript:jumpToValue(6)">6 </a>
    </body>
    </html>

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Posts
    150
    Thanks for that but let say that i enter a 9 after the 6 so i want the first 2 digit 2 be "69" so it will go into your combobox and find them

    Is that code working only with the first number found ?


    EX

    Value in the combox:
    6196
    6297
    6398
    6499
    6585
    6995

    ok so you click on the comboBox and press 6 after 9 so after doing so i would like the cursor to go on 6995.

    Thanks

  5. #5
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    indexOf returns the position of the letter eg
    if we use the number 6789

    indexOf(6) returns 0
    indexOf(7) returns 1
    indexOf(8) returns 2
    indexOf(9) returns 3

    Now that you know how to access the values of the combo box and how to match letters you should be able to figure it for your self.

    If you can't post in detail what you want the user to be able to do and I'll post a solution !

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Posts
    150
    Thanxs for that, very helpfull

    Let say i want to send the key that the user press as a parameter, is there a way i can do that?

    Thanxs

  7. #7
    DeadEyes
    Guest
    Have a look at this when you type anything in the text box the combobox gets updated. Hope it helps, with what progressive
    shown you your practiclly there.
    Code:
    <script>
    function Index(obj,cbo){
    	var oStr = new String(obj.value);
    	var tmpStr;
    	for(i=0;i<cbo.length;i++){
    		tmpStr = new String(cbo.options[i].text);
    		tmpStr = tmpStr.substring(0,oStr.length)
    		if (oStr.toUpperCase()==tmpStr.toUpperCase()){
    			cbo.options[i].selected = true;
    			return true;
    		}
    	}
    	return true;
    }
    </script>
    	<form name="combo">
    	<table>
    		<tr>
    			<td>
    			 <input type="text" name="txtSearch"  onkeyup="Index(this,box);">
    				<br>
    				<select id="box" name="box" size=8>
    					<option  value="1" selected>appple</option>
    					<option  value="2">banana</option>
    					<option  value="3">cat</option>
    					<option  value="3">cob</option>
    					<option  value="3">coconut</option>
    					<option  value="4">kiwi</option>
    					<option  value="5">orange</option>
    				</select>
    			</td>
    		</tr>
    	</table>
    	</form>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width