Jump to content

Parallel Arrays ?


Elemental

Recommended Posts

Hey Folks, Not sure how to word this so please bare with me...I have a form with five checkboex and each checkbox has a corresponding select options list:checkbox_One / Select_Onecheckbox_Two / Select_Two.....checkbox_Five / Select_FiveEach Select Option starts with a value of 0 and can go up to 25.What type of an Array would I need to create in order to know that if, say, checkbox_One is checked then a value greater than 0 for Select_One has to be selected also in order for the form to validate?I would think it's a for loop Array but I don't know what type of an Array this could be. I learned, using the tutorials, how to make the user select at least one checkbox if they want to validate the form but how do I do the two??Or I should say what type of an Array do I need to create for this??Does my question make sense?Peace,Elemental

Link to comment
Share on other sites

Why an array? Are you making this harder than it has to be?You look at each checkbox. (That can be a loop.) For each box, you get the value of the selected option and see if it's > 0. Simple, yes?Are you not sure how to associate a given checkbox with a particular select element?I'm just not seeing the problem you're trying to solve. Maybe if you clarify that first instead of assuming an array is part of the solution.

Link to comment
Share on other sites

Why an array? Are you making this harder than it has to be?You look at each checkbox. (That can be a loop.) For each box, you get the value of the selected option and see if it's > 0. Simple, yes?Are you not sure how to associate a given checkbox with a particular select element?I'm just not seeing the problem you're trying to solve. Maybe if you clarify that first instead of assuming an array is part of the solution.
Deirdre's Dad, Thanks for the reply,Why an array? Are you making this harder than it has to be?I guess I am, I thought an array would be the way to go with the multiple checkboxes and select elementsAre you not sure how to associate a given checkbox with a particular select element?Not sure of the question, so I guess my answer is yes.Unless you mean something like this?:if (checkbox_One.checked = true && selecT_One.option <= 0){alert(please select a quantity); .....I'm just not seeing the problem you're trying to solve. Maybe if you clarify that first instead of assuming an array is part of the solution.One day, and I'm working on soon, I'll be able to post clearer more intelligent questions but until then, thank you for your patience and knowledge.Peace,Elemental
Link to comment
Share on other sites

Just work with the DOM. It has all the collections (arrays) you need. You just need to know how to access them. What we have here is some sample markup for ONE checkbox and its select element. They are associated simply by being at the same level of the same div. If you want more, don't add more divs at the same level as stuff; add more divs like the one contained by "stuff," the one that contains all the inputs.Anyway, run this, figure out how it works. Then play with it. Like, you'll want to add names and ids and stuff.

<div id="stuff">	<div>		<input type="checkbox">		<select>			<option value="0">Select a number</option>			<option value="1">1</option>			<option value="2">2</option>		</select>	</div>	<button type="button" onclick="validate()">Validate</button> <!-- THIS IS EXTRA, JUST FOR TESTING --> </div>

function validate () {	 boxes = document.getElementById("stuff").getElementsByTagName("input");	 for (var i = 0; boxes[i]; i++) {		 if (boxes[i].checked == true) {			 var sel = boxes[i].parentNode.getElementsByTagName("select")[0];			 var val = sel.options[sel.selectedIndex].value;			 if (val > 0) {				 alert ("validates");			 }		 }	 } }

Link to comment
Share on other sites

Just work with the DOM. It has all the collections (arrays) you need. You just need to know how to access them. What we have here is some sample markup for ONE checkbox and its select element. They are associated simply by being at the same level of the same div. If you want more, don't add more divs at the same level as stuff; add more divs like the one contained by "stuff," the one that contains all the inputs.Anyway, run this, figure out how it works. Then play with it. Like, you'll want to add names and ids and stuff.
<div id="stuff">	<div>		<input type="checkbox">		<select>			<option value="0">Select a number</option>			<option value="1">1</option>			<option value="2">2</option>		</select>	</div>	<button type="button" onclick="validate()">Validate</button> <!-- THIS IS EXTRA, JUST FOR TESTING --> </div>

function validate () {	 boxes = document.getElementById("stuff").getElementsByTagName("input");	 for (var i = 0; boxes[i]; i++) {		 if (boxes[i].checked == true) {			 var sel = boxes[i].parentNode.getElementsByTagName("select")[0];			 var val = sel.options[sel.selectedIndex].value;			 if (val > 0) {				 alert ("validates");			 }		 }	 } }

Deirdre's Dad, Thank you for the education Sir, I will certainly study the example you posted in your reply.I wasn't expecting that; thank you for taking the time and going out of your way, much appreciated.Peace,Elemental
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...