View Full Version : Inserting blank option in select

02-23-2007, 06:22 PM
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:

<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 value="412">CA Agency Disclaimer (v.050412.412)(H)</option>
<option value="1239">Laws and Regulations (v.070213.1239)(H)</option>
<option value="1241">Laws and Regulations (v.070213.1241)(H)</option>
<option value="1249">Laws and Regulations (v.070214.1249)(H)</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!

02-23-2007, 07:34 PM
"i" is not incrementing by one. So try something like:

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?

02-23-2007, 08:03 PM
Thanks a ton, it works perfect now!

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!

02-23-2007, 08:16 PM