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!

limit checkboxes that are passed to a drop down

Status
Not open for further replies.

casinoweirdo

Technical User
Oct 18, 2006
3
US
I have the following code and I need to limit the amount of checkboxes to 5. Can anyone help me?

<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)}
else {
boxes = document.forms['question2'].question2.value
addOption(document.forms['question2'].question4, 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)}
while (document.forms['question2'].question4.options.length > 1) {
document.forms['question2'].question4.remove(1)

}
}

</script>

<form name="question2">
<table width="955">
<tr>
<td> <p class="MsoNormal">What 5 benefits seem to appeal to your customer the most?
&nbsp;&nbsp;(Please check 5 only)</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Personal Host">&nbsp;&nbsp;&nbsp; Personal Host</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Guaranteed value parking">&nbsp;&nbsp;&nbsp; Guaranteed valet parking</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Food Discounts">&nbsp;&nbsp;&nbsp; Food Discounts</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "VIP Lounge">&nbsp;&nbsp;&nbsp; VIP Lounge</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "High percentage of cash back">&nbsp;&nbsp; &nbsp; High percentage of cash back</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Free food">&nbsp;&nbsp;&nbsp; Free food</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Bonus entries for drawings">&nbsp;&nbsp;&nbsp; Bonus entries for drawings</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) 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(17) value = "Free hotel stay">&nbsp;&nbsp;&nbsp; Free hotel stay</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) 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(17) value = "Online access to your account">&nbsp;&nbsp;&nbsp; Online access to your account</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Monthly guaranteed comp hotel and food program">&nbsp;&nbsp;&nbsp; Monthly guaranteed comp hotel and food program</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Invitations to exclusive special events">&nbsp;&nbsp;&nbsp; Invitations to exclusive special events</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Upgrade to suite hotel comps">&nbsp;&nbsp;&nbsp; Upgrade to suite hotel comps</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Hotel discount rates">&nbsp;&nbsp;&nbsp; Hotel discount rates</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Monthly rewards based on level of play">&nbsp;&nbsp;&nbsp; Monthly rewards based on level of play</p>
<p class="MsoNormal">&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="question2" onclick=moveIt(17) value = "Gift shop discounts">&nbsp;&nbsp;&nbsp; Gift shop discounts</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">
<option></option>
</select>


<p class="MsoNormal">What 1 benefit do you find lease appealing?</p><br />
<select name="question4">
<option></option>
</select>

</td>
</tr>
</table>
</form>
 
The changes I'll post below will work. However, a few things should be said about your code. You definitely should not name the form AND the checkboxes "question2". Although it may help you distinguish what group they belong to when you view the code, it's very misleading to have multiple names for different html elements that don't belong together as a group. Naming all the checkboxes with the same name makes sense, it's a group of checkboxes - but giving that same name to the form is very misleading, and it will break commands like getElementsByName because it would return everything. Second, make sure you enclose the function call from the onclick handlers in quotes. Anyway, this should get it working:
Code:
<script type="text/javascript">
    function moveIt(numboxes, obj) {
[!]        var totalBoxesChecked = 0;
        for (i = 0; i < numboxes; i++) {
           if (document.forms["question2"].elements["question2"][i].checked) {
              totalBoxesChecked++;
           }
           if (totalBoxesChecked > 5) {
              alert("I told you to only pick 5!!!");
              obj.checked = false;
              return false;
           }
        }[/!]
        var boxes = new Array(numboxes)
        clearSelect()
        for (i = 0; i < numboxes ; ++i) {
            if (document.forms['question2'].question2[i].checked == true) {
                boxes[i] = document.forms['question2'].question2[i].value
                addOption(document.forms['question2'].question3, boxes[i], boxes[i], boxes[i])}
            else {
                 boxes[i] = document.forms['question2'].question2[i].value
                addOption(document.forms['question2'].question4, boxes[i], boxes[i], boxes[i])
            }
        }
    }
    
    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)}
        while (document.forms['question2'].question4.options.length > 1) {
            document.forms['question2'].question4.remove(1)

        }
     }

</script>

<form name="question2">
<table width="955">
    <tr>
        <td>        <p class="MsoNormal">What 5 benefits seem to appeal to your customer the most?
&nbsp;&nbsp;(Please check 5 only)</p>
        <p class="MsoNormal">&nbsp;</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Personal Host">&nbsp;&nbsp;&nbsp; Personal Host</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Guaranteed value parking">&nbsp;&nbsp;&nbsp;                         Guaranteed valet parking</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Food Discounts">&nbsp;&nbsp;&nbsp; Food Discounts</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "VIP Lounge">&nbsp;&nbsp;&nbsp; VIP Lounge</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "High percentage of cash back">&nbsp;&nbsp;                &nbsp; High percentage of cash back</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Free food">&nbsp;&nbsp;&nbsp; Free food</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Bonus entries for drawings">&nbsp;&nbsp;&nbsp;             Bonus entries for drawings</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" 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(17[!], this[/!])" value = "Free hotel stay">&nbsp;&nbsp;&nbsp;                     Free hotel stay</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" 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(17[!], this[/!])" value = "Online access to your account">&nbsp;&nbsp;&nbsp; Online access to your account</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Monthly guaranteed comp hotel and food program">&nbsp;&nbsp;&nbsp; Monthly guaranteed comp hotel and food program</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Invitations to exclusive special events">&nbsp;&nbsp;&nbsp; Invitations to exclusive special events</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Upgrade to suite hotel comps">&nbsp;&nbsp;&nbsp; Upgrade to suite hotel comps</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Hotel discount rates">&nbsp;&nbsp;&nbsp; Hotel discount rates</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Monthly rewards based on level of play">&nbsp;&nbsp;&nbsp; Monthly rewards based on level of play</p>
        <p class="MsoNormal">&nbsp;&nbsp;&nbsp;
        <input type="checkbox" name="question2" onclick="moveIt(17[!], this[/!])" value = "Gift shop discounts">&nbsp;&nbsp;&nbsp; Gift shop discounts</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">
            <option></option>
            </select>
            
            
          <p class="MsoNormal">What 1 benefit do you find lease appealing?</p><br />
            <select name="question4">
            <option></option>
            </select>

        </td>
    </tr>
</table>
</form>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top