0

I have javascript code that reads from an xml that contains a category node and populates a dropdown list with it. That category node is created every time the user creates a new product listing. However, this means that when the xml is read, the category is filled with items such as Camera, Camera, Camera, Others. How do I remove that or is there a distinct option?

function loadXMLcat()
{
var xmlhttp=false;
 if (window.XMLHttpRequest)
{
  xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","auction.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
var category;
var str;
var i;
var j;
str=("<p>Category : <select onchange=\"reveal(this)\" name=category>");
var x=xmlDoc.getElementsByTagName("Product");
if(x.length>0)
{
    for (i=0;i<x.length;i++)
    {
        category=xmlDoc.getElementsByTagName("Category");
        str+="<option value=" + category[i].childNodes[0].nodeValue + ">" + category[i].childNodes[0].nodeValue + "</option>"
        /**var arr = category;
        var sorted_arr = arr.sort()
        var results = [];
        for (var i = 0; i < arr.length - 1; i++) 
        {
            if (sorted_arr[i + 1] !== sorted_arr[i]) 
            {
                str+="<option value=" + arr[i] + ">" + arr[i] + "</option>"
                str+="<option value=" + sorted_arr[i] + ">" + sorted_arr[i] + "</option>"
            }
        }**/
    }
    str+="<option id=\"others\" value=\"Other\">Other</option>";
}
else
str+="<option id=\"others\" value=\"Other\">Other</option>";
str+=("</select></p>");
document.getElementById('category').innerHTML=str;
}
JianYA
  • 2,750
  • 8
  • 60
  • 136

1 Answers1

0

Here is the same loop with a check to see if the category was already added to the select box. I have also added the contains() function to quickly test the duplicates.

function loadXMLcat()
{
  var xmlhttp=false;
  if (window.XMLHttpRequest)
  {
    xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("GET","auction.xml",false);
  xmlhttp.send();
  xmlDoc=xmlhttp.responseXML; 

  var str;
  var i;
  var j;
  str=("<p>Category : <select onchange=\"reveal(this)\" name=category>");
  var categoryList= []; //Array to contains the distinct values
  var x=xmlDoc.getElementsByTagName("Product");
  var categories = xmlDoc.getElementsByTagName("Category");
  if(x.length>0)
  {
    for (i=0;i<x.length;i++)
    {
        var category=categories[i].childNodes[0].nodeValue;
        var currentOption ="<option value=" + category + ">" + category  + "</option>";
        //Verify is category has already be added
        if(!contains(categoryList,category)){
            categoryList.push(category);
            str += currentOption;
        }
    }
    str+="<option id=\"others\" value=\"Other\">Other</option>";
  }
  else
    str+="<option id=\"others\" value=\"Other\">Other</option>";
  str+=("</select></p>");
  document.getElementById('category').innerHTML=str;
}

//Utility function to quickly verify if an array contains an element.
function contains(arr, x) {
    return arr.filter(function(elem) { return elem == x }).length > 0;
}