Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Location
    Cincinnati, OH
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs down Inserting blank option in select

    Code:
    document.getElementById('selectedChapter').options.length = 0;
    var content = xmlHttp.responseText.split(\"*%*\");
    for(var i=0;i<content.length;i=i+2){
         document.getElementById('selectedChapter').options[i] = new Option(content[i+1],content[i]);
    }
    I am running an AJAX script and the following little piece of code is used to build a select list with the results I am getting back from the server.

    For whatever reason that I cannot for the life of me figure out, it is putting an empty option tag after each actual option it adds to the list as seen in the following:

    Code:
    <select id="selectedChapter" onchange="chapterSelect(document.getElementById('selectedChapter').value);" style="font-family: monospace;" name="selectedChapter" size="15" 0="multiple">
    <option value="0">-Create Chapter-</option>
    <option/>
    <option value="412">CA Agency Disclaimer (v.050412.412)(H)</option>
    <option/>
    <option value="1239">Laws and Regulations (v.070213.1239)(H)</option>
    <option/>
    <option value="1241">Laws and Regulations (v.070213.1241)(H)</option>
    <option/>
    <option value="1249">Laws and Regulations (v.070214.1249)(H)</option>
    <option/>...
    Any ideas what might be causing this? When I put an alert in my for-loop it doesn't alert anything out about the empty option so I don't understand where it is coming from.

    Any help would be greatly appreciated!

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "i" is not incrementing by one. So try something like:
    Code:
      for(var i=0, j=0;i<content.length;i=i+2, ++j){
        document.getElementById('selectedChapter').options[j] = new Option(content[i+1],content[i]);
      }
    Btw... doesn't this \"*%*\" cause a syntax error?

  • #3
    New Coder
    Join Date
    Jul 2006
    Location
    Cincinnati, OH
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a ton, it works perfect now!

    Quote Originally Posted by MikeFoster View Post
    Btw... doesn't this \"*%*\" cause a syntax error?
    No, that is what I am putting in my string to split it on. I had a comma at first but some of the chapters had commas in them so I just went with something that will never show up in there, thus I went with *%*.

    Thanks again!

  • #4
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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