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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! I have a clickable map. wh

Status
Not open for further replies.

greygirl

Programmer
Jun 12, 2002
34
US
Help!
I have a clickable map. when I click on the different areas the areas are added into a list box. Now I am trying to avoid duplicate values in the list box. This is my javascript to add values into the list box. Can somebody tell me how to avoid duplicate values in this list box with this script.

function copyItem(name)
{
var newOpt = new Option();
newOpt.value = name;
newOpt.text = name;
var n = document.mainForm.select1.length;
document.mainForm.select1.options[n] = newOpt;

}

Thanks for any help.

greygirl
 
hey,

just change your function to include a test for if the option name already exists as follows...

Code:
function copyItem(name)
{
if (!document.mainForm.select1.options[name])
  {
    var newOpt = new Option();
    newOpt.value = name;
    newOpt.text = name;
    var n = document.mainForm.select1.length;
    document.mainForm.select1.options[n] = newOpt;
   }        
}

glenn
 
Glenn
That didn't work. Any other tricks. Thanks
greygirl
 
This should check the existing values, and exit the function without doing anything if the value already exists. Otherwise, it'll use your original code to add to the list.

function copyItem(name)
{
for (var oi=0;oi<document.mainForm.select1.options.length;oi++)
{
if document.mainForm.select1.options[oi].value==name) return;
}

var newOpt = new Option();
newOpt.value = name;
newOpt.text = name;
var n = document.mainForm.select1.length;
document.mainForm.select1.options[n] = newOpt;
}
 
Hi
That didn't work either. I still could click the same county many times and it adds all of them to the list box.
greygirl

 
I didn't see a typo in the 'if' statement. What am I missing here?
greygirl
 
oops! I saw it. It worked now. Thanks!
greygirl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top