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!

Get Selected Checkboxes/innerHTML

Status
Not open for further replies.

SPrelewicz

Programmer
Jul 16, 2001
124
US
This may be long...

Problem - I am dynamically creating some form elements on a page based on prewvious selection.

Step 1 - User clicks an area on a map, Group of checkboxes is created based on selection. name=state id-state. this I have working no problem using a <div> and innerHTML() in that div.

The user then should be able to select a few state checkboxes and Then a lsit of cities should appear in another <div> via a multi select.

Code -

Code:
function loadCities() {
   var buttonGroup=document.foo.state;
   var string; 

   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { 
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
            string += "state=" + buttonGroup[selectedItems[i]].value + ";";

      }
   }
  

    if (typeof window.ActiveXObject != 'undefined' ) {
      xmlDoc2 = new ActiveXObject("Microsoft.XMLHTTP");
      xmlDoc2.onreadystatechange = processCities;
    }
    else {
      xmlDoc2 = new XMLHttpRequest();
      xmlDoc2.onload = processCities;
    }

    xmlDoc2.open( "GET", "/cgi-bin/xmlmoc.cgi?action=cities&"+string, true );
    xmlDoc2.send( null );
}

function processCities() {
	var cityArray=new Array();
	var valArray;
	var string_out;
	document.getElementById('Cities').innerHTML="<select name='cities' id='cities' MULTIPLE>";

      if (xmlDoc2.readyState == 4) {
        // only if "OK"
        if (xmlDoc2.status == 200) {
            delX= ":";
            delY= ",";
            valArray = xmlDoc2.responseText.split(delX);
            var valArraylen = valArray.length - 1;
	    string_out += "Select Your Cities:<br>";
            for (var i=0; i <= valArraylen; i++ ) {
                cityArray = valArray[i].split(delY);
		if (cityArray[1] == undefined) {continue;}
 		string_out += "<option name=city value="+ cityArray[0] +">" + cityArray[1];
             } 
        } else {
            alert("There was a problem retrieving the data:" + xmlDoc2.statusText);
        }

    }
	document.getElementById('Cities').innerHTML= string_out;
	document.getElementById('Cities').innerHTML= "</select>";	
	
}

My specific error is happening at -
var selectedItems = getSelectedCheckbox(buttonGroup);

I guess its not seeing the state checkbox group? -
var buttonGroup=document.foo.state;

Am I missing something, or is it not being seen because the checkbox group itself was created via a similar function with innerHTML?

Thanks in advance.

Scott
 
Apologies. here tis

Code:
function getSelectedCheckbox(buttonGroup) {
   var returnArray = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { 
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            returnArray.length = lastElement;
            returnArray[lastElement] = i;
            lastElement++;
         }
      }
   } else { 
      if (buttonGroup.checked) {
         returnArray.length = lastElement;
         returnArray[lastElement] = 0; 
      }
   }
   return returnArray;
}

About me at Flying Roman.com lyric meaning and discussion
Prelewic, Renassance Man ;)
 
first thing i'd do is a simple
Code:
alert(document.foo.state);
i always avoid referring to objects in that manner. rather, i do it this way (what is foo, by the way, is it a form?):
Code:
document.forms['foo'].elements['state'];
if foo is not a form, try this:
Code:
document.getElementById('state');

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top