Results 1 to 7 of 7

Thread: Select lists - remember values after submit not working in IE7

  1. #1
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Select lists - remember values after submit not working in IE7

    I have two dynamic select lists that are populated using .csv files.

    On submit, a set of results is returned based on the two options that were chosen.

    In IE7 when the results are returned the select lists default back to the last option in the select list. I would like it to default to what ever option was just chosen.

    I'm using the following code and it works as I would like it to in both Firefox and Safari, but just defaults to the last option in the list for IE7.

    Code:
    <script language="JavaScript" type="text/javascript">
       // Used to be sure the last item selected stays selected.
          if (document.getElementById("litType_" + actualLitType) != null)
          {
               document.getElementById("litType_" + actualLitType).selected=true;
          }	
      </script>
    The variable "actualLitType" is pulled from the URL and should be part of the id of the selected option.

    Any ideas why it doesn't work like I would like it to in IE7?

    Thanks in advance,
    Jesse

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    For IE you may need to save and restore the select element's options' selectedIndex. The bad news is that if you are unfamiliar with what that means, it's a little complicated. The good news is that other browsers can do it this way too, so you won't need two sets of code.

    Let's say that actualLitType is the value of the selected option and litType is the id of the select element:

    HTML Code:
    <select id="litType" name="actualLitType">
    <option value="english">English</option>
    <option value="french">French</option>
    <option value="spanish">Spanish</option>
    </select>
    Then you could do:

    Code:
    var o = document.getElementById('litType').options;
    for (var i = o.length - 1; i > -1; --i)
    if(o[i].value == actualLitType)
    o.selectedIndex = i;
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    jgifford (08-13-2008)

  4. #3
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    That did it!

    Thanks John.

  5. #4
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I have another related issue.

    This time I have a select list dynamically populated by a SQL Server database.

    I want to select all items in the list when a particular item is chosen. The code I've written works great in Firefox and I'm assuming other browsers as well. It just doesn't work in IE and specifically tested not working in IE7.

    Here is the code I'm using:

    Select List:
    Code:
    <select name="Literature_MarketID" class="requiredfield" size="13" id="marketList" multiple>
            <option selected="selected" value="">=== Control-click to select multiple items ===</option>
                <%
    While (NOT rsMarkets.EOF)
    %>
                <option value="<%=(rsMarkets.Fields.Item("Markets_ID").Value)%>" id="marketOption_<%=(rsMarkets.Fields.Item("Markets_ID").Value)%>" onClick="checkAll();"><%=(rsMarkets.Fields.Item("Markets_Description").Value)%></option>
                <%
      rsMarkets.MoveNext()
    Wend
    If (rsMarkets.CursorType > 0) Then
      rsMarkets.MoveFirst
    Else
      rsMarkets.Requery
    End If
    %>
              </select>
    Javascript used to select all options when "All" is chosen from the list
    Code:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function checkAll()
    {
    	var allMarkets = document.getElementById("marketOption_13");
    
    			if (allMarkets.selected == true)
    			{
    				if (document.getElementById('marketList') != null)
    				{
    					var o = document.getElementById('marketList');
    					for (var i = o.length - 1; i > 0; --i)
    					{
    							o.options[i].selected = true;
    					}
    				}	
    			}
    }
    //-->
    </script>
    I can't use the fix suggested for my last issue because this time I want all options selected and selectedIndex only applies to one option.

    Any ideas on what I can do to make this work in IE7?

  6. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the following code, which makes multiple selection in a select list in both FF and IE:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<title>Untitled Document</title>
    		<style type="text/css">
    		
    		</style>
    		<script type="text/javascript">
    		function multipleSelection(){
    			var s = document.forms['form1'].elements['sel1'];
    			s.multiple = true;
    			for(var i = 0; i < s.options.length; i++){
    				s.options[i].selected = true;
    			}
    		}
    		</script>
    	</head>
    	<body>
    		<div>
    			<form name="form1">
    				<select name="sel1">
    					<option value="1">One</option>
    					<option value="2">Two</option>
    					<option value="3">Three</option>
    					<option value="4">Four</option>
    					<option value="5">Five</option>
    				</select>
    				<input type="button" name="b1" value="click to multiple selection"/>
    			</form>
    		</div>
    		<script type="text/javascript">
    		document.forms['form1'].elements['b1'].onclick = multipleSelection;
    		</script>
    	</body>
    </html>
    Hope this helps.
    Last edited by codeexploiter; 08-20-2008 at 09:37 AM. Reason: correction

  7. #6
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    codeexploiter:

    That is a pretty cool trick. I changed my code so it looked more like yours, but it still doesn't appear to be working in IE.

    Any ideas what part of my code IE doesn't like?

    Some differences between my code and yours where the IE issue may exist:

    1. Yours starts as a regular select list and is changed to a multiple. Mine needs to start as a multiple and stay a multiple.
    2. Yours selects all when the button is clicked. Mine needs to select all only when a specific option is clicked.

    Here is my updated code:
    Javascript used to select all options when "All" is chosen from the list
    Code:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function checkAll()
    {
    var allMarkets = document.getElementById("marketOption_13");
    
    	if (allMarkets.selected == true)
    	{
    
    		var o = document.forms['addLiterature'].elements['Literature_MarketID'];
    		o.multiple = true;
    
    			for(var i = 0; i < o.options.length; i++)
    			{
    				o.options[i].selected = true;
    			}
    	}
    }
    //-->
    </script>
    Select List:
    Code:
    <select name="Literature_MarketID" class="requiredfield" size="13" id="marketList" multiple>
            <option selected="selected" value="">=== Control-click to select multiple items ===</option>
                <%
    While (NOT rsMarkets.EOF)
    %>
                <option value="<%=(rsMarkets.Fields.Item("Markets_ID").Value)%>" id="marketOption_<%=(rsMarkets.Fields.Item("Markets_ID").Value)%>" onClick="checkAll();"><%=(rsMarkets.Fields.Item("Markets_Description").Value)%></option>
                <%
      rsMarkets.MoveNext()
    Wend
    If (rsMarkets.CursorType > 0) Then
      rsMarkets.MoveFirst
    Else
      rsMarkets.Requery
    End If
    %>
    </select>
    Thanks for all the help,
    Jesse
    Last edited by jgifford; 08-20-2008 at 03:35 PM. Reason: Forgot some code

  8. #7
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb

    I was given a solution in another forum and it did the trick. Thanks everyone for your help. Here is where the solution is:

    http://www.tek-tips.com/viewthread.c...1495671&page=1

Bookmarks

Posting Permissions

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