Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm getting a blank option in my dropdownlist

Status
Not open for further replies.

MayoorPatel

Programmer
Apr 10, 2006
35
GB
Hi there I have a javascript array which is populated by the database
Code:
<script language="Javascript" type="text/javascript">
<!--
var subTextArray = new Array();
var subValueArray = new Array();
subTextArray[4] = new Array();
subValueArray[4] = new Array();
subTextArray[4][1] = "4.1 Agriculture1";
subValueArray[4][1] = 122;
subTextArray[4][3] = "4.3 Agriculture3";
subValueArray[4][3] = 123;
//-->
</script>

as you can see the array has been populated with 2 elements from the db and they are indexed 4.1 and 4.3 because I deleted 4.2 earlier. The code that reads these arrays and puts together the actual dropdown is here.

[/code]
<script type="text/javascript">
function populate(catSelect,formName,subSelectName,selected)
{
subSelect = eval("document.forms." + formName + "." + subSelectName);
subSelect.options.length = 0;
var catNum = catSelect.options[selected].value;

if (subTextArray[catNum] == null)
{
var noneOption = new Option("-- No Subcategories --", "-1");
subSelect.options[0] = noneOption;
return;
}

var allOption = new Option("-- Choose a Subcategory --", "-1");
subSelect.options[0] = allOption;
for (var i = 1; i < subTextArray[catNum].length; i++)
{
// there might be blank ones, so skip them out
if (subTextArray[catNum] != null)
{
var tmpOption = new Option(subTextArray[catNum], subValueArray[catNum]);
subSelect.options = tmpOption;
}
}
}
</script>
[/code]

as you can see it loops through the arrays and outputs the dropdown. However Because I deleted 4.2 earlier the code does not take account of this and produces this.


which causes me major problems later on, not to mention the fact it looks ugly. Can anyone help me understand how I could ammend my code so that it cuts out the blank lines?

Mayoor
 
Try testing typeof array element for "undefined" instead of null
Code:
if ( typeof subTextArray[catNum][i] == "undefined" ) {
  //skip this one
} else {
  //create this one
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top