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

creating two separate drop downs from one group of checkboxes 1

Status
Not open for further replies.

casinoweirdo

Technical User
Oct 18, 2006
3
US
my background isn't in java so I need some help.

Is there a way to create a drop down from a list of checkboxes that aren't checked?

I've got 12 checkboxes and I've created a drop down from the ones the user selects, but I need to be able to create a survey questions from the ones that weren't selected.

Here's what I've got so far

<script type="text/javascript">
function moveIt(numboxes) {
var boxes = new Array(numboxes)
clearSelect()
for (i = 0; i < numboxes ; ++i) {
if (document.forms['question2'].question2.checked == true) {
boxes = document.forms['question2'].question2.value
addOption(document.forms['question2'].question3, boxes, boxes, boxes, boxes, boxes, boxes, boxes, boxes, boxes, boxes, boxes, boxes)
}
}
}

function addOption(selectbox,text,value ){
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

function clearSelect(){
while (document.forms['question2'].question3.options.length > 1) {
document.forms['question2'].question3.remove(1)
}
}

</script>

<form name="question2">
<table>
<tr>
<p class="MsoNormal">What 5 benefits seem to appeal to your customer the most?
&nbsp;&nbsp;(Please check 5 only)</p>
</tr>
<tr>
<td>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Personal Host">&nbsp;&nbsp;&nbsp; Personal Host</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Guaranteed value parking">&nbsp;&nbsp;&nbsp; Guaranteed valet parking</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Food Discounts">&nbsp;&nbsp;&nbsp; Food Discounts</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Bonus Entries">&nbsp;&nbsp;&nbsp; Bonus entries for drawings</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Priority seating in our food restaurants">&nbsp;&nbsp; &nbsp; Priority seating in our food restaurants</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Free hotel stay">&nbsp;&nbsp;&nbsp; Free hotel stay</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Cash back rewards using earned points">&nbsp;&nbsp;&nbsp; Cash back rewards using earned points</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Online access to their account">&nbsp;&nbsp;&nbsp; Online access to their account</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Invistations to special events">&nbsp;&nbsp;&nbsp; Invitations to special events</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Generous comping program">&nbsp;&nbsp;&nbsp; Generous
comping program</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Hotel discount rates">&nbsp;&nbsp;&nbsp; Hotel discount rates</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(12) value = "Monthly coin offers">&nbsp;&nbsp;&nbsp; Monthly coin offers</p>

<br /><br />
<p class="MsoNormal">Of the above, what 1 benefit do you think your customers value the most?</p><br />
<select name="question3">
</select>
</td>
</tr>
</table>
</form>




 
It looks like you almost have what you want.
What is the particular problem you are having as that would be easier to address.

Note:
In this line:
Code:
addOption(document.forms['question2'].question3, boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i])
You are repeating boxes many times to no point. Your function is only setup to take three values and all of the extra boxes are just repeats.

Also, you pass in the number of boxes to check but this is not really needed. Since all of your checkboxes have the same name you are already receiving them as an array and you are just looping through that array testing if they have been checked. You could just as easily do this:
Code:
var chkboxes = document.forms['question2'].question2;
for (i = 0; i < chkboxes.length; ++i) {
  if (chkboxes[i].checked) {
.......
}

Testing for checked returns true or false so you do not need to use == true.
However, since you want to test if it is NOT checked you would use something like:
Code:
if (!chkboxes[i].checked) {
This way the if statement only passes if the box is NOT checked.


At my age I still learn something new every day, but I forget two others.
 
I'm confused. You said " Your function is only setup to take three values and all of the extra boxes are just repeats." I need the function to be able to take only 5 values.
 
Your function call is this:
Code:
addOption(document.forms['question2'].question3, boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i], boxes[i])

You are passing the object for question3 as your first parameter, then you have 12 instances of boxes.

This function:
Code:
    function addOption(selectbox,text,value ){
        var optn = document.createElement("OPTION");
        optn.text = text;
        optn.value = value;
        selectbox.options.add(optn);
    }
Only wants three parameters, selectbox, text and value.

You have your for loop setup to go through checkboxes question2[0] through question2[12] which is actually 13 times through the loop so the last time through is actually going to give you an error.

Your addOption function requires the object it needs to populate the options into and the text and value for the option. Your loop will call that function 13 times adding the options one at a time. The last time will cause an error because you will be trying to get a value for question2[12] which does not exist. Arrays start at 0 not at 1 so to check 12 fields you loop 0 to 11.



At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top