Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Validation: Get an unchecked radio group's name?

    Hi.

    I did find some threads that are similar to this, but nothing quite answers what I need.

    I'm working on validating radio buttons in a form. I want to require that a button be selected from required radio button groups before accepting the submit. Not all the radio button groups are required to have a selection.

    One problem I'm having is that I can't return the name of the radio button group. I want to switch case on the name of the radio button group.

    I do have an id for each individual radio button, so I can confirm that the for loops are working, but my alert tells me that radiogroups[j] is an [object] and radiogroups[j].name is undefined.

    And there is a problem with the logic, too. If just one of the radio button sets is checked, the form validates.

    What I've got so far:

    Code:
    function validateFields(){ // these are my required radio button fields
      	var radiogroups = new Array(
    	 document.forms[0].profile,
    	 document.forms[0].chargetuition,
    	 document.forms[0].speced,	 
    	 document.forms[0].prek1,
    	 document.forms[0].prek2,
    	 document.forms[0].monitorperf,
    	 document.forms[0].ckpat,
    	 document.forms[0].k8,
    	 document.forms[0].training,
    	 document.forms[0].natconf
      	);
      	var radioselected = false;
    
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < radiogroups[j].length; k++) { 
    			if (radiogroups[j][k].checked == true)    {
    				radioselected = true;
    				alert(radiogroups[j][k].id + " is checked in group " + radiogroups[j].name);
    		   	}// if true
    		} // for radiobuttons k
    		if (!radioselected)  {
    			alert(radiogroups[j].name + " has no selection");
            	var errdisplay = document.getElementById('error_' + radiogroups[j].name);
    			switch(radiogroups[j]) {
    				case "profile" :
    					var errormessage = "New profile or update?";
    					showError(errormessage, errdisplay)
    					break;
    				case "chargetuition" :
    					var errormessage = "Do you charge tuition?";
    					showError(errormessage, errdisplay)
    					break;
    				case "speced" :
    					var errormessage = "Are you using Core Knowledge with special needs children?";
    					showError(errormessage, errdisplay)
    					break;
    				case "prek1" :
    					var errormessage = "Do you use the entire Preschool Sequence with Level 1 classes?";
    					showError(errormessage, errdisplay)
    					break;
    				case "prek2" :
    					var errormessage = "Do you use the entire Preschool Sequence with Level 2 classes?";
    					showError(errormessage, errdisplay)
    					break;
    				case "monitorperf" :
    					var errormessage = "Does your school use assessments?";
    					showError(errormessage, errdisplay)
    					break;
    				case "ckpat" :
    					var errormessage = "Do you use the CK-PAT?";
    					showError(errormessage, errdisplay)
    					break;
    				case "k8" :
    					var errormessage = "Is your school using Core Knowledge in grades K8?";
    					showError(errormessage, errdisplay)
    					break;
    				case "training" :
    					var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    					showError(errormessage, errdisplay)
    					errdisplay.appendChild(showerror);
    					break;
    				case "natconf" :
    					var errormessage = "Have you attended a national conference?";
    					showError(errormessage, errdisplay)
    					break;
    				default:
    					break;
    			} //switch
    			return false;
    	 	 } // if !radioselected 
    
    	} // for radiogroups j
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }
    thanks!!!

    ---Opally

  • #2
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Also tried radiogroups[j][k].name but got an error saying it was null or not an object.

    But if radiogroups[j][k].id correctly returns the id of the element, why doesn't radiogroups[j].name or radiogroups[j][k].name return the name of the element?

    I did define radiogroups[j] as an array of radio element names, so I'm befuddled.

    The form can be seen here.

  • #3
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Got this working, but the logic is still not right, the form passes on to the review screen if just one radio button group is checked, instead of all.

    Code:
    function validateFields(){
    
      	var radiogroups = new Array(
    	 "profile",
    	 "chargetuition",
    	 "speced",	 
    	 "prek1",
    	 "prek2",
    	 "monitorperf",
    	 "ckpat",
    	 "k8",
    	 "training",
    	 "natconf"
      	);
      	var radioselected = false;
    	var myform = document.prekprofile;
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < myform[radiogroups[j]].length; k++) { 
    			if (myform[radiogroups[j]][k].checked == true)    {
    				radioselected = true;
    				alert(myform[radiogroups[j]][k].id + " is checked in group " + radiogroups[j]);
    		   	}// if true
    		} // for radiobuttons k
    		if (!radioselected)  {
    			alert(radiogroups[j] + " has no selection");
            	var errdisplay = document.getElementById('error_' + radiogroups[j]);
    			switch(radiogroups[j]) {
    				case "profile" :
    					var errormessage = "New profile or update?";
    					showError(errormessage, errdisplay)
    					break;
    				case "chargetuition" :
    					var errormessage = "Do you charge tuition?";
    					showError(errormessage, errdisplay)
    					break;
    				case "speced" :
    					var errormessage = "Are you using Core Knowledge with special needs children?";
    					showError(errormessage, errdisplay)
    					break;
    				case "prek1" :
    					var errormessage = "Do you use the entire Preschool Sequence with Level 1 classes?";
    					showError(errormessage, errdisplay)
    					break;
    				case "prek2" :
    					var errormessage = "Do you use the entire Preschool Sequence with Level 2 classes?";
    					showError(errormessage, errdisplay)
    					break;
    				case "monitorperf" :
    					var errormessage = "Does your school use assessments?";
    					showError(errormessage, errdisplay)
    					break;
    				case "ckpat" :
    					var errormessage = "Do you use the CK-PAT?";
    					showError(errormessage, errdisplay)
    					break;
    				case "k8" :
    					var errormessage = "Is your school using Core Knowledge in grades K8?";
    					showError(errormessage, errdisplay)
    					break;
    				case "training" :
    					var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    					showError(errormessage, errdisplay)
    					errdisplay.appendChild(showerror);
    					break;
    				case "natconf" :
    					var errormessage = "Have you attended a national conference?";
    					showError(errormessage, errdisplay)
    					break;
    				default:
    					break;
    			} //switch
    			return false;
    	 	 } // if !radioselected 
    
    	} // for radiogroups j
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }

  • #4
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I figure I need to add a counter, to count whether all the radiobutton groups have selections, and when they do, it's valid.

    Is there a technique to return a "focus" to a radio button group? Embed named anchors and... how do I call the page with the named anchor appended, hmm.

    So far, my error message is displaying in its span, but it displays even if the radio button is selected, so I still have a logic problem... still thinking about this...

    Code:
    function validateFields(){
    
      	var radiogroups = new Array(
    	 "profile",
    	 "chargetuition",
         "speced",	 
    	 "prek1",
         "prek2",
    	 "monitorperf",
    	 "ckpat",
    	 "k8",
    	 "training",
         "natconf"
      	);
      	var radioallselected = 0;
    	var radiovalid = false;
    	var myform = document.prekprofile;
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < myform[radiogroups[j]].length; k++) { 
    			if (myform[radiogroups[j]][k].checked == true)    {
    				radioallselected++;
    				alert(myform[radiogroups[j]][k].id + " is checked in group " + radiogroups[j] + "\n\nradioallselected = " + radioallselected);
    		   	}// if true
    		} // for radiobuttons k
    		radiovalid = (radioallselected == radiogroups.length ? true : false);
    		if (radiovalid) {
    			break;
    		} else if (!radiovalid)  {
    			alert(radiogroups[j] + " has no selection");
            	var errdisplay = document.getElementById('error_' + radiogroups[j]);
    			switch(radiogroups[j]) {
    				case "profile" :
    					var errormessage = "New profile or update?";
    					showError(errormessage, errdisplay);
    					break;
    				case "chargetuition" :
    					var errormessage = "Do you charge tuition?";
    					showError(errormessage, errdisplay)
    					break;
    				case "speced" :
    					var errormessage = "Are you using Core Knowledge with special needs children?";
    					showError(errormessage, errdisplay)
    					break;
    				case "prek1" :
    					var errormessage = "Do you use the entire Preschool Sequence with Level 1 classes?";
    					showError(errormessage, errdisplay)
    					break;
    				case "prek2" :
    					var errormessage = "Do you use the entire Preschool Sequence with Level 2 classes?";
    					showError(errormessage, errdisplay)
    					break;
    				case "monitorperf" :
    					var errormessage = "Does your school use assessments?";
    					showError(errormessage, errdisplay)
    					break;
    				case "ckpat" :
    					var errormessage = "Do you use the CK-PAT?";
    					showError(errormessage, errdisplay)
    					break;
    				case "k8" :
    					var errormessage = "Is your school using Core Knowledge in grades K8?";
    					showError(errormessage, errdisplay)
    					break;
    				case "training" :
    					var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    					showError(errormessage, errdisplay)
    					errdisplay.appendChild(showerror);
    					break;
    				case "natconf" :
    					var errormessage = "Have you attended a national conference?";
    					showError(errormessage, errdisplay)
    					break;
    				default:
    					break;
    			} //switch
    			return false;
    	 	 } // if !radioselected 
    
    	} // for radiogroups j
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by Opally View Post
    how do I call the page with the named anchor appended, hmm.
    Code:
    location.hash = "#anchorName";
    Quote Originally Posted by Opally View Post
    So far, my error message is displaying in its span, but it displays even if the radio button is selected, so I still have a logic problem... still thinking about this...
    It's because you are not setting the corresponding error span to empty. Previous error msg might be in the span so you need to always set it to empty when radio button selection is valid.

    Code:
    if (radiovalid) {
      //reset corresponding error here
      break;
    } else if (!radiovalid)  {
      ...
    }

  • #6
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanks for the help, Glenn!! I will incorporate your suggestions.

    I have another problem with the script so far: I don't believe it's seeing the length of the radio button array. It only evaluates correctly if the first radio button in the set is checked, the second radio button is invisible.

    Specifically, myform[radiogroups[j]].length is not evaluating the number of radio buttons in the group.

    What I've got so far, with a counter to check how many radio button groups are valid:

    Code:
    function validateFields(){
     
    
      	var radiogroups = new Array(
    	 "profile",
    	 "chargetuition",
         	 "speced",	 
    	 "monitorperf",
    	 "prek1",
         	 "prek2",
    	 "ckpat",
    	 "k8",
    	 "training",
         	 "natconf"
      	);
      	var radioallselected = 0;
    	var radiovalid = false;
    	var myform = document.prekprofile;
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < myform[radiogroups[j]].length; k++) { 
    			if (myform[radiogroups[j]][k].checked == true)    {
    				radioallselected++;
    				alert(myform[radiogroups[j]][k].id + " is checked in group " + radiogroups[j] + "\n\nradioallselected = " + radioallselected);
    		   	}// if true
    			if (radioallselected == j) { // no buttons selected in j
    				alert(radiogroups[j] + " has no selection");
            		var errdisplay = document.getElementById('error_' + radiogroups[j]);
    				switch(radiogroups[j]) {
    					case "profile" :
    						var errormessage = "New profile or update?";
    						showError(errormessage, errdisplay);
    						break;
    					case "chargetuition" :
    						var errormessage = "Do you charge tuition?";
    						showError(errormessage, errdisplay)
    						break;
    					case "speced" :
    						var errormessage = "Are you using Core Knowledge with special needs children?";
    						showError(errormessage, errdisplay)
    						break;
    					case "monitorperf" :
    						var errormessage = "Does your school use assessments?";
    						showError(errormessage, errdisplay)
    						break;
    					case "prek1" :
    						var errormessage = "Do you use the entire Preschool Sequence with Level 1 classes?";
    						showError(errormessage, errdisplay)
    						break;
    					case "prek2" :
    						var errormessage = "Do you use the entire Preschool Sequence with Level 2 classes?";
    						showError(errormessage, errdisplay)
    						break;
    					case "ckpat" :
    						var errormessage = "Do you use the CK-PAT?";
    						showError(errormessage, errdisplay)
    						break;
    					case "k8" :
    						var errormessage = "Is your school using Core Knowledge in grades K8?";
    						showError(errormessage, errdisplay)
    						break;
    					case "training" :
    						var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    						showError(errormessage, errdisplay)
    						errdisplay.appendChild(showerror);
    						break;
    					case "natconf" :
    						var errormessage = "Have you attended a national conference?";
    						showError(errormessage, errdisplay)
    						break;
    					default:
    						break;
    				} //switch
    				return false;
    			} // no buttons selected in j
    		} // for radiobuttons k
    		radiovalid = (radioallselected == radiogroups.length ? true : false);
    		if (radiovalid) {
    			break;
    		} else continue ;
    	} // for radiogroups j
    
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }
    form is here.

    ---Opally

  • #7
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    kewl, this ALMOST does what I want it to. Thank you for the anchor focus and look how I dealt with cleaning up the error message: nothing like recycling!

    Except I am not getting the .length of the radio button element.
    Code:
    for (var k=0; k < myform[radiogroups[j]].length; k++) {
    Code:
    function validateFields(){
    
      	var radiogroups = new Array(
    	 "profile",
    	 "chargetuition",
         	 "speced",	 
    	 "monitorperf",
    	 "prek1",
         	 "prek2",
    	 "ckpat",
    	 "k8",
    	 "training",
         	 "natconf"
      	);
      	var radioallselected = 0;
    	var radiovalid = false;
    	var myform = document.prekprofile;
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < myform[radiogroups[j]].length; k++) { 
            	var errdisplay = document.getElementById('error_' + radiogroups[j]);
    			if (myform[radiogroups[j]][k].checked == true)    {
    				radioallselected++;
    				alert(myform[radiogroups[j]][k].id + " is checked in group " + radiogroups[j] + "\n\nradioallselected = " + radioallselected);
    				showError('',errdisplay);
    		   	}// if true
    			if (radioallselected == j) { // no buttons selected in j
    				alert(radiogroups[j] + " has no selection");
    				var anchorname = "#" + radiogroups[j] + "anchor";
    				switch(radiogroups[j]) {
    					case "profile" :
    						var errormessage = "New profile or update?";
    						showError(errormessage, errdisplay);
    						location.hash = anchorname;
    						break;
    					case "chargetuition" :
    						var errormessage = "Do you charge tuition?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "speced" :
    						var errormessage = "Are you using Core Knowledge with special needs children?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "monitorperf" :
    						var errormessage = "Does your school use assessments?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "prek1" :
    						var errormessage = "Do you use the entire Preschool Sequence with Level 1 classes?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "prek2" :
    						var errormessage = "Do you use the entire Preschool Sequence with Level 2 classes?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "ckpat" :
    						var errormessage = "Do you use the CK-PAT?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "k8" :
    						var errormessage = "Is your school using Core Knowledge in grades K8?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "training" :
    						var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "natconf" :
    						var errormessage = "Have you attended a national conference?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					default:
    						break;
    				} //switch
    				return false;
    			} // no buttons selected in j
    		} // for radiobuttons k
    		radiovalid = (radioallselected == radiogroups.length ? true : false);
    		if (radiovalid) { // reset error mesages
    			
    			break;
    		} else continue ;
    	} // for radiogroups j
    
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    What does myform[radiogroups[j]].length say if you alert it?

    Try changing it to:
    Code:
    myform.elements[radiogroups[j]].length

  • #9
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks, Glenn.

    That suggestion works: myform.elements[radiogroups[j]].length does return the correct length of the radio button group.

    However, the loop is still not evaluating if the second radio button is checked.

    It only evaluates myform.elements[radiogroups[j]][k].checked == true if the first button is checked, not the second.

    I put in some more diagnostic alerts but still haven't figured it out.

    Code:
    function validateFields(){
    
      	var radiogroups = new Array(
    	 "profile",
    	 "chargetuition",
    	 "speced",	 
    	 "monitorperf",
    	 "ckpat",
    	 "prek1",
    	 "prek2",
    	 "k8",
    	 "training",
    	 "natconf"
      	);
      	var radioallselected = 0;
    	var radiovalid = false;
    	var myform = document.prekprofile;
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < myform.elements[radiogroups[j]].length; k++) { 
            	var errdisplay = document.getElementById('error_' + radiogroups[j]);
    			alert("myform.elements[radiogroups[j]].length = " + myform.elements[radiogroups[j]].length + "\n\n k = " + k); 
    			if (myform.elements[radiogroups[j]][k].checked)    {
    				radioallselected++;
    				alert(myform[radiogroups[j]][k].id + " is checked in group " + radiogroups[j] + "\n\nradioallselected = " + radioallselected);
    				showError('',errdisplay);
    		   	}// if true
    			if (radioallselected == j) { // no buttons selected in j
    				alert("radioallselected = " + radioallselected + "\n\n j = " + j);
    				alert(radiogroups[j] + " has no selection");
    				var anchorname = "#" + radiogroups[j] + "anchor";
    				switch(radiogroups[j]) {
    					case "profile" :
    						var errormessage = "New profile or update?";
    						showError(errormessage, errdisplay);
    						location.hash = anchorname;
    						break;
    					case "chargetuition" :
    						var errormessage = "Do you charge tuition?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "speced" :
    						var errormessage = "Are you using Core Knowledge with special needs children?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "monitorperf" :
    						var errormessage = "Does your school use assessments?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "ckpat" :
    						var errormessage = "Do you use the CK-PAT?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "prek1" :
    						var errormessage = "Do you use the entire Preschool Sequence with Level 1 classes?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "prek2" :
    						var errormessage = "Do you use the entire Preschool Sequence with Level 2 classes?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "k8" :
    						var errormessage = "Is your school using Core Knowledge in grades K8?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "training" :
    						var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "natconf" :
    						var errormessage = "Have you attended a national conference?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					default:
    						break;
    				} //switch
    				return false;
    			} // no buttons selected in j
    		} // for radiobuttons k
    		radiovalid = (radioallselected == radiogroups.length ? true : false);
    		if (radiovalid) { // reset error mesages
    			
    			break;
    		} else continue ;
    	} // for radiogroups j
    
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }

  • #10
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    hmm, it *is* a loop logic problem... will see if I can sort it out...

    ---Opally

  • #11
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I think I got it! I had to move the closing bracket of the innermost loop up so it was correctly testing all the radio button conditions before testing whether there was no selection.

    THANK YOU for all your SPLENDID help!!!

    ---Opally

    Code:
    function validateFields(){
      	var radiogroups = new Array(
    	 "profile",
    	 "chargetuition",
    	 "speced",	 
    	 "monitorperf",
    	 "ckpat",
    	 "seq",
    	 "k8",
    	 "training",
    	 "natconf"
      	);
      	var radioallselected = 0;
    	var radiovalid = false;
    	var myform = document.prekprofile;
      	for (var j = 0; j < radiogroups.length; j++)  {
    		for (var k=0; k < myform.elements[radiogroups[j]].length; k++) { 
            	var errdisplay = document.getElementById('error_' + radiogroups[j]);
    //			alert("myform.elements[radiogroups[j]].length = " + myform.elements[radiogroups[j]].length + "\n\n k = " + k); 
    			if (myform.elements[radiogroups[j]][k].checked)    {
    				radioallselected++;
    //				alert(myform[radiogroups[j]][k].id + " is checked in group " + radiogroups[j] + "\n\nradioallselected = " + radioallselected);
    				showError('',errdisplay);
    		   	}// if true
    		} // for radiobuttons k
    			if (radioallselected == j) { // no buttons selected in j
    //				alert("radioallselected = " + radioallselected + "\n\n j = " + j);
    //				alert(radiogroups[j] + " has no selection");
    				var anchorname = "#" + radiogroups[j] + "anchor";
    				switch(radiogroups[j]) {
    					case "profile" :
    						var errormessage = "New profile or update?";
    						showError(errormessage, errdisplay);
    						location.hash = anchorname;
    						break;
    					case "chargetuition" :
    						var errormessage = "Do you charge tuition?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "speced" :
    						var errormessage = "Are you using Core Knowledge with special needs children?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "monitorperf" :
    						var errormessage = "Does your school use assessments?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "ckpat" :
    						var errormessage = "Do you use the CK-PAT?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "seq" :
    						var errormessage = "Do you use the entire Preschool Sequence?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "k8" :
    						var errormessage = "Is your school using Core Knowledge in grades K8?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "training" :
    						var errormessage = "Has your preschool had Core Knowledge Preschool Training?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					case "natconf" :
    						var errormessage = "Have you attended a national conference?";
    						showError(errormessage, errdisplay)
    						location.hash = anchorname;
    						break;
    					default:
    						break;
    				} //switch
    				return false;
    			} // no buttons selected in j
    		radiovalid = (radioallselected == radiogroups.length ? true : false);
    		if (radiovalid) { // reset error mesages
    			
    			break;
    		} else continue ;
    	} // for radiogroups j
    
     return true;
    
    } 
    
    function showError(message, errdisp) {
    	var showerror = document.createTextNode(message);
    	while(errdisp.lastChild) { // remove any childs from errdisplay
    		errdisp.removeChild(errdisp.firstChild);
    	}					
    	errdisp.appendChild(showerror);
    }

  • #12
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    oops... having a problem with showHideOnload() function.

    I had a small modification, a simplification of my form, and I thought this should be working fine... but no divs are being hidden on page reload. The initial load works correctly, and the selection of the buttons works, but reload of the page after review displays all divs.

    The alert is telling me that radGroup is an [object] and doesn't seem to be evaluating.

    There is one div, "seqomit", which displays if radio group name="sequence" "no" is selected. All the others display if "yes" is selected for their respective radio buttons.

    Code:
    function showHideOnload(choiceName, divId) {
    	var frm = document.forms[0];
    	var radGroup = frm.elements[choiceName];
    	var choice = getSelectedRadio(radGroup);
    alert("radGroup = " + radGroup + " choiceName = " + choiceName + "\n\n divID = " + divId);
    	if (choiceName.indexOf("sequence")!= -1){
    		if (!choice || choice.value=="yes") {
    			document.getElementById(divId).style.display = 'none';
    		}
    		else if (choice.value=="no"){
    			document.getElementById(divId).style.display = 'inline';
    		}
    		
    	}
    	else {
    		document.getElementById(divId).style.display = (choice) ? 'inline':'none';
    	}
    }
    
    
    function getSelectedRadio(radGroup){
      for (var i=0; i<radGroup.length; i++){
         if (radGroup[i].checked) return radGroup[i];
      }
      return null; //nothing selected
    }
    
    function initDivs(){
      var arr = new Array(
         ['sequence', 'seqomit'],
    	 ['monitorperf','assmtsfields'],
    	 ['ckpat','ckpathowlong'],
    	 ['k8','ckgrades'],
    	 ['training','modules'],
         ['natconf', 'natconfyears'] //no comma at the last element
      );
    
      for (var i=0; i<arr.length; i++){
         showHideOnload(arr[i][0], arr[i][1]);
      }
    }

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Is the link to the form you posted in post#2 updated? I tried it there and I saw that when I select yes on some of the radio buttons, the corresponding div is displayed. And when I reload the page, the form is reset, no radio button is selected by default and no divs are displayed.

  • #14
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Glenn,

    Thanks for following up!!

    Please try filling out the form and submitting, to set the session. Then return to the form (choose "edit your information" at the review screen.) When the restored form loads, all the divs show. That's the reload condition, you'll see the alerts.

    this is the form

    What's odd is, I thought this actually was working. I only made a simplification of the previous prek1/prek2 logic, if you remember that. Now it's just a radio button group name="sequence" that, unlike the others, if "yes" is chosen, hides the div. All the others in the array for initDivs work the other way.

    whatcha think?

    ---Opally

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Change this:
    Code:
    else {
      document.getElementById(divId).style.display = (choice) ? 'inline':'none';
    }
    to:
    Code:
    else {
      document.getElementById(divId).style.display = (choice && choice.value=="yes") ? 'inline':'none';
    }
    As far as I can remember, that's what I did when I initially posted that showHideOnload function. You may want to change 'inline' to 'block' as div is a block element not inline.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •