Results 1 to 8 of 8

Thread: Simple question on javascript

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Location
    Pittsburgh, PA
    Posts
    149
    How do I remove all the options in a select. basically I want to remove all the set options and then repopulate it. I can get it to populate.

  2. #2
    Member
    Join Date
    Aug 2000
    Location
    Florida
    Posts
    45
    How are you populating it? If it's dynamic then it always starts from an empty list.
    If I could only remember my name...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Location
    Pittsburgh, PA
    Posts
    149
    I am populating it through a javascript so it can reset the values in the list. I want to erase the list when a new option is selected and then repopulate it. If I don't dumb the list, it keep some of the old options if the list is shorter.

  4. #4
    Member
    Join Date
    Aug 2000
    Location
    Florida
    Posts
    45
    Are the values hard-coded? If so you could set up conditions in combining your html and script e.g.

    pseudo-code...

    <select>
    if (condition1)
    <OPTION value="bob">Bob
    <OPTION value="jimbob">Jim
    Else
    <OPTION value="jo">Jo
    <OPTION value="david">David
    </select>
    If I could only remember my name...

  5. #5
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    if you have a look at http://www.parkers.co.uk
    select used car prices and then view the code

    it does exactly what you want.

    It looks a though it is re-populating iover original values and then re-sizing the select box
    Mark
    -------------------

  6. #6
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    You need to loop through starting at the last option element. That should keep the resizing of it from causing an error.

    Code:
    for i = frm.lstSelect.Options.Length - 1 to 0 Step -1
        frm.lstSelect.Options.Remove i
    Next
    oOOo--oOOo
    __/\/\onte96
    oOOo--oOOo
    Senior Programmer/Analyst
    MCP
    [email protected]
    [email protected]


    Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Except in JavaScript of course
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Code:
    <HEAD>
    <script Language="javaScript">
    var i;
    function fillList()	
    {
    	i++;		
    	var oOption;
    	oOption = document.createElement("OPTION");	
    	oOption.text="Apples";	
    	oOption.value=i;
    	document.frm1.select1.add(oOption);
    }
    
    function clearList()
    {
    
    document.frm1.select1.length = 1;
    i=0;
    
    }
    </script>
    </head>      
    <BODY>
    <form name="frm1">            
    <SELECT id=select1 name=select1>
    <option>Please select</option>    
    </SELECT>              
    </form>          
    <input type="button" onclick="fillList()" value="Add item">  
    <input type="button" onclick="clearList()" value="clear list" id=button1 name=button1>         
    </BODY></HTML>
    [Edited by Mark Sreeves on 09-13-2000 at 03:36 AM]
    Mark
    -------------------

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