www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 05-18-2006, 01:51 AM
    heals1ic heals1ic is offline
    Registered User
     
    Join Date: Sep 2004
    Posts: 19
    IE AJAX select tag options

    I have a script that involves replacing dropdown options via ajax and javascript:

    Code:
    <script type="text/javascript">
    var url = "pref.php?catid="; // The server-side script
    function handleHttpResponse() {	
    	if (http.readyState == 4) {
    		if(http.status==200) {
    			var results=http.responseText;
    			var resultarray = new Array();
    			resultarray = results.split("|", 3);
    			for (var i=0; i<3; i++) {
    				document.getElementById('pref_'+(i+1)).innerHTML = resultarray[i];
    			}
    		}
    	}
    }
    
    function requestPrefs() {
    	var cat = document.getElementById('catselect');
    	var id = cat.options[cat.selectedIndex].value;
    	http.open("GET", url + escape(id) + "&userid=<?=$_SESSION['user_id']?>", true);
    	http.onreadystatechange = handleHttpResponse;
    	http.send(null);
    }
    function getHTTPObject() {
    	var xmlhttp;
    	if(window.XMLHttpRequest){
    		xmlhttp = new XMLHttpRequest();
    	}
    	else if (window.ActiveXObject){
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		if (!xmlhttp){
    			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    		}
    	}
    	return xmlhttp;
    }
    var http = getHTTPObject(); // We create the HTTP Object
    </script>
    <form action="pref.php" method="post">
    <select id="catselect" size="7" name="catid" onChange="requestPrefs();">
    <option value="1">cat1</option>
    <option value="2">cat2</option>
    </select>
    <select id="pref_1" name="pref_1">
    <option value="">-- Select --</option>
    </select>
    <select id="pref_2" name="pref_2">
    <option value="">-- Select --</option>
    </select>
    <select id="pref_3" name="pref_3">
    <option value="">-- Select --</option>
    </select>
    The data returned in the results variable is a string of option elements with the selected option in this string. The string is separated by a delimeter to allocate the 3 option lists into arrays for the 3 select dropdowns.

    This code works fine for Firefox but will not populate the select elements in IE.
    Reply With Quote
      #2  
    Old 05-19-2006, 07:23 PM
    phpnovice phpnovice is offline
    Banned
     
    Join Date: Dec 2004
    Posts: 8,652
    Without running this through a script debugger, I can only surmise two possible reasons why this is failing. Either this:

    document.getElementById('pref_'+(i+1)).innerHTML = resultarray[i];

    should be more like this:

    document.forms[0].elements['pref_'+(i+1)].innerHTML = resultarray[i];

    or, IE just doesn't like the use of innerHTML to create the Options object within a SELECT.
    Reply With Quote
      #3  
    Old 05-20-2006, 12:21 AM
    heals1ic heals1ic is offline
    Registered User
     
    Join Date: Sep 2004
    Posts: 19
    I have a feeling that innerHTML (with reguards to select tags) is an issue for IE even though by definition it's use meets all the criteria. Typical of Microsoft.

    Would it be a better option for the server script to produce XML elements and then parse these into the select dropdowns from the client side? How then can I have options in these dropdowns preselected? It was easier to predefine the option elements from the server side before transferring them to the client.

    My original intention was to be as light on in processing for the client side as possible. No too many iterations of arrays etc. Seems like IE is going to be a pain in the ass though.

    Any ideas?
    Reply With Quote
      #4  
    Old 05-20-2006, 09:03 AM
    phpnovice phpnovice is offline
    Banned
     
    Join Date: Dec 2004
    Posts: 8,652
    To hear some people speak, they would condemn you for using innerHTML in the first place. At any rate... The only additional thing I have to offer you is that you can replace the entire SELECT using the method you've already got, or you can go to iterative processing and use this as your base reference:

    var opt = document.forms[0].elements['pref_'+(i+1)].options;

    and this as your iterative process:

    opt[opt.length] = new Option(strText, strValue, blnDefaultSelected, blnSelected);
    Reply With Quote
      #5  
    Old 05-20-2006, 09:44 AM
    heals1ic heals1ic is offline
    Registered User
     
    Join Date: Sep 2004
    Posts: 19
    Quote:
    To hear some people speak, they would condemn you for using innerHTML in the first place
    More information on this?
    What if any, is the preferred method then?

    Am I barking up the wrong tree to solve this issue?
    Reply With Quote
      #6  
    Old 05-20-2006, 02:35 PM
    phpnovice phpnovice is offline
    Banned
     
    Join Date: Dec 2004
    Posts: 8,652
    Quote:
    Originally Posted by heals1ic
    Quote:
    Originally Posted by phpnovice
    To hear some people speak, they would condemn you for using innerHTML in the first place.
    More information on this?
    What if any, is the preferred method then?
    Oh, you're getting me started... The supposed "purists" have a tendancy to turn up their collective noses at any innovative ideas MS comes up with on its own. The supposed "purists" think that if it doesn't come from the W3C first, then it is "bad" practice and "invalid" usage. The W3C, themselves, has this problem, too. Why do you think they call IE standard operations "quirks" mode? There's nothing intrinsically wrong with the way IE does things. IE was doing it long before there were standards defining any other way to do it. Because of its forgiving nature, it is the easiest browser to design for because of those very "quirks" at which the supposed "purists" turn up their noses.

    Case in point:
    http://www.webdeveloper.com/forum/sh...ad.php?t=61505

    So, we come to innerHTML... This allows script to quickly and efficiently create, modify, and remove dynamic HTML elements. In their obstinancy, the W3C apparently wants this to only be done using approved methods and in OO fashion. Not that there's anything intrinsically wrong with OO, but we're talking about an interpreted language here and, thus, there isn't the luxury in response time and performance that you'd have with a compiled language. In short, performance and response time is slower if you stick to the approved methods for creating, modifying, and removing dynamic HTML elements. In fact, I've heard that the W3C is having to bow to pressure from other sources and is (finally) including innerHTML in the standards.
    Quote:
    Originally Posted by heals1ic
    Am I barking up the wrong tree to solve this issue?
    No, of course not. I already gave you the solution -- replace the entire SELECT element using innerHTML (not just the options), or go back to an iterative process and construct each Option one at a time.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:29 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.