Web Analytics Made Easy -
StatCounter selectedIndex not working in IE - CodingForum

Announcement

Collapse
No announcement yet.

selectedIndex not working in IE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • selectedIndex not working in IE

    Hi, can anyone help me with this script -

    Code:
    function selectCountry(){
    obj = document.getElementById("Country");
    for(i=0; i<obj.length; i++){
    	if(obj.options[i].value == "<?php echo $Country; ?>"){
    		obj.selectedIndex = i;
    		return;
    		}
    	}	
    }
    it is supposed to (and in ff does) select the value in the drop down based on the php $country value.
    The list has over 200 elements and is not dynamically generated (hence why I'm using js rather than php to determine which value should be selected)

    I tried adding
    Code:
    obj.options[i].selected == true;
    after setting the selectedIndex and I got an unspecified error, can anyone help?
    http://www.hazelryan.co.uk

  • #2
    This works as expected with IE6


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function selectCountry(){
    obj = document.getElementById("Country");
    for(i=0; i<obj.length; i++){
    	if(obj.options[i].value == "fred"){
    		obj.selectedIndex = i;
    		return;
    		}
    	}
    }
    //-->
    </script>
    </head>
    
    <body  onload="selectCountry();">
    <select id="Country" >
    <option value="tom">Tom</option>
    <option value="dick">Dick</option>
    <option value="harry">Harry</option>
    <option value="fred">Fred</option>
    </select>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

    Comment


    • #3
      Here is a link to the live code - feel free to test it http://www.njsims.com/Calendar/addedit.php?id=7
      The country should be Switzerland, but (for me at least) it says United Kingdom (this is as expected if nothing is selected, also if I remove the 'selected' from the html for UK then Afghanistan will be displayed in IE)
      http://www.hazelryan.co.uk

      Comment


      • #4
        I have solved the issue by removing about 50 items from the coutries list - IE craps out with so much data.
        http://www.hazelryan.co.uk

        Comment


        • #5
          your like worked for me but try

          <body onload="selectCountry();">

          as my example rather than leaving the call hanging at the bottom of the page
          Vic

          God Loves You and will never love you less.

          http://www.vicsjavascripts.org/Home.htm

          If my post has been useful please donate to http://www.operationsmile.org.uk/

          Comment

          Working...
          X