Drop-down list from text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lantamer
    New Member
    • Aug 2006
    • 1

    Drop-down list from text file

    Hi

    I need to make script that creates drop-down list from text file (new line in text file – new line in drop-down list). Somebody told me that I should use Ajax and XMLHttpRequest for that, so I have found on internet script that can write content from text file on screen, but I don’t know how to modify it to write it in drop-down list.

    How to add reference.value to make values for drop-down?

    This is it:
    Code:
    <html>
    <head>
    <title>XMLHttpRequest Object GET Request</title>
    </head>
    <body>
    
    <script language="javascript" type="text/javascript">
    <!--
    
      // request variable
      var request_var;
    
      // Request object method wrapper function
      function request_object()
      {
        try
        {
          return new ActiveXObject('Msxml2.XMLHTTP');
        }
          catch(e)
          {
            try
            {
            return new ActiveXObject('Microsoft.XMLHTTP');
            }
              catch(e)
              {
              return new XMLHttpRequest();
              }
          }
      }
    
      // Call the request object method wrapper function
      request_var=request_object();
      
      if(!request_var)
      {
        alert("Your Web browser does not support the XMLHttpRequest object.");
      }
    
      function load()
      {
        if(request_var.readyState==4)
        {
        document.getElementById('elements_id').innerHTML=request_var.responseText;
        }
      }
    
      function call()
      {
        if(request_var)
        {
        d=document;
        request_var.open("GET","textfile.txt",true);
        request_var.onreadystatechange=load;
        request_var.send(null);
        }
      }
    
    //-->
    </script>
    
    <div id="elements_id"></div> 
    
    <a onclick="call()" href="#">XnMLHttpRequest Object GET Request</a>
    </body>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The changes that are required are:
    1. parse the response instead of writing it to the screen. The response is a string. Split it into an array using the split() method.
    2. Loop over the array to add the options. You can use the options array (and new Option()) or the select object's add() method to add options.

    Comment

    Working...