Add selection into listbox checking for duplicates

wobbled
wobbled used Ask the Experts™
on
Hi,

I have a page (MAIN.ASP) which has an IFRAME within it.  IFRAME loads page (SUB_PAGE.ASP)

In sub_page.asp there is a form and when a user selects a checkbox a javascript function writes the value to a text box on MAIN.ASP

What I wish to do

1) Instead of writing it to a textbox, I would like it to add it to a list box on MAIN.ASP.  
         The listbox is called: lst_countryName,
         The form is called: country_1

2) I need to check if that value already exists within the listbox and not allow duplicates  
         The listbox should have values as the country ID(which are in the variable cbResults) and will be unique for each country
          Also should show the name of the country as the label (which are in variable strCountry )

In short:  I need javascript to add values from an Iframed page into the parent page listbox, without adding duplicates.

Thanks in advance

//code from within the iframe page
 
function loopForm() {
    var cbResults = '';
	var strCountry = '';
    //var radioResults = 'Radio buttons: ';
    for (var i = 0; i < country.elements.length; i++ ) {
        if (country.elements[i].type == 'checkbox') {
            if (country.elements[i].checked == true) {
                cbResults += country.elements[i].value + ',';
				strCountry += country.elements[i].name + ', ';
            }
        }
      
    }
	
	cbResults = cbResults.slice(0, -1)	//trim off trailing comma
	strCountry = strCountry.slice(0, -2)
	document.country.sel_ctry_id.value = cbResults;	
	document.country.cty_name.value = strCountry;
	
	//add them to the parent page - text box
	parent.document.getElementById('cty_name_1').value = strCountry;
	parent.document.getElementById('sel_ctry_id_1').value = cbResults;
 
       //here is where I wish to add it to the listbox - both the cbResults which are IDs and the strCountry which is the name of the country
 
 
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If you could post in the full HTML, I think I can give you a working version of what you're looking for pretty quickly. I just don't have time to recreate your forms and such that power it.
Put the following into where you want it to be. It will just work :D
       //here is where I wish to add it to the listbox - both the cbResults which are IDs and the strCountry which is the name of the country
        var list = parent.document.getElementById('lst_countryName');
        var existing = false;
        for (var i = 0; i < list.options.length; i++) {
            if (list.options[i].value == cbResults) {
                existing = true; // one already exists
                break;
            }
        }
        if (!existing) { // only create element and add into the list if none exists
            var op = parent.document.createElement('option');
            op.text = strCountry;
            op.value = cbResults;
            list.appendChild(op);
        }

Open in new window

Please use this version as it contains a fix for IE. IE doesn't use text but label attribute.
       //here is where I wish to add it to the listbox - both the cbResults which are IDs and the strCountry which is the name of the country
        var list = parent.document.getElementById('lst_countryName');
        var existing = false;
        for (var i = 0; i < list.options.length; i++) {
            if (list.options[i].value == cbResults) {
                existing = true; // one already exists
                break;
            }
        }
        if (!existing) { // only create element and add into the list if none exists
            var op = parent.document.createElement('option');
            op.text = strCountry;
            op.label = strCountry;
            op.value = cbResults;
            list.appendChild(op);
        }

Open in new window

Author

Commented:
Excellent!!! - Did the job.  I had to make a small change though and added:

list.options[list.options.length] = op;

Instead of

list.appendChild(op);

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial