...

View Full Version : using check boxes in IF...THEN statements



noviceNate
01-10-2003, 04:53 PM
I have a check box on a form for users to check if they are a certain age.

I need to be able to verify that they checked it. If they did, then "this function" happens, otherwise, "that function" happens. Can you help me out please? Thanks!!

Mr J
01-10-2003, 08:51 PM
Try The following


<script language="javascript">
<!--
chkbox= new Array()

function check(){
for (i = 0; i < document.f2.length; i++) {
if (document.f2[i].type == "checkbox"){
chkbox[i]=i
if(document.f2.elements[chkbox[i]].checked==true){
alert("checkbox "+document.f2[i].name+" is checked")
}
else{
alert("checkbox "+document.f2[i].name+" is NOT checked")
}
}
}
}
// -->
</script>


<form name="f2">
18 <input type="checkbox" name="18">
21 <input type="checkbox" name="21">
<input type="button" value="Test" onclick="check()">
</form>

noviceNate
01-13-2003, 05:48 PM
That didn't help...but I think I need to just show you the code and what I'm wanting. Should have done that in the first place.

<form name="FrontPage_Form1">
<input type="text" name="name1"> <input type="checkbox" name="name1_type" value="child">

...


<input type="text" name="reserved_name1">
...

</form>

------------------------------------------------------------


The ... means that the input fields would be duplicated in order down to name10, name10_type, and reserved_name10.

From this, here's how I want it to work. Someone enters their name into the first text field, name1. If they are an adult, they will not click the check box next to it, and the value for name1 will automatically populate the value for reserved_name1. However, if the name they enter is for a child, they will check the checkbox. If this is so, it will still populate the reserved_name1 field with the value of name1, but it will also go to all caps.

So that let's say someone fills it out and enters 6 adult names and 4 children's names. The 4 children's names will be in all-caps. Does this make any sense? More importantly, can it be done?? I really would appreciate any help. thanks so much in advance!!

whammy
01-14-2003, 01:22 AM
It can be done, but we'd need to see the form in its entirety and have a little clearer explanation of the logic involved, preferably without code. :)

noviceNate
01-14-2003, 06:04 PM
I don't know how I'm supposed to show you the form without showing code. But I'll try to explain a little more detail...you asked for it.


The form is for registration for a banquet. First the user has to tell us who all is going to the banquet. They must also mark whether the person in their party is a child or an adult. The people registering can also make requests on who they want to sit with at their table. Each table sits 10 people. We want the form to work like this...

When a user enters the names of the people in their party into one or more of 10 open textboxes, of course they want to sit with them, so those names will auto-populate the other textboxes at the bottom of the form where the user can make requests on who to sit with.

For example...I am Doug, and I'm bringing my wife Brenda and our kids Charlie and Olphilia. So I enter our names, and check the boxes next to Charlie and Olphilia's to indicate they are children. The first 4 textbox fields in the "reserve seating" section at the bottom of the form automatically fills in Doug, Brenda, Charlie, and Olphilia. However, because Charlie and Olphilia are children, their names will appear in all-caps or italic. Then I can enter my friends' names, Greg and Betty, in two of the other 6 open spots in the reserve seating section. Does this make sense??

I dont know what else I can say to make it any clearer than that. Please ask though if you need more clarification. Thanks

arnyinc
01-14-2003, 07:58 PM
Is there a specific reason you have to do it like that? If so, my answer is useless, but it might be an easier alternative.

It seems a bit strange that you have to duplicate the values in one set of text fields into another set of text fields. Can you just have a text field and a select box associated with each of these fields to specify whether the field is an adult, child, or friend?


<html>
<head>
<script language="javascript">
function italicize(gname, gtype){
if ((gtype.options[gtype.selectedIndex].text)=="Child")
gname.style.fontStyle='italic';
else
gname.style.fontStyle='normal';
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="guest_name_1">
<select name="guest_type_1" onchange="italicize(document.myform.guest_name_1, this)">
<option>Adult</option>
<option>Child</option>
<option>Preferred Friend</option>
</select>
<br>
<input type="text" name="guest_name_2">
<select name="guest_type_2" onchange="italicize(document.myform.guest_name_2, this)">
<option>Adult</option>
<option>Child</option>
<option>Preferred Friend</option>
</select>
</form>

</body>
</html>

noviceNate
01-14-2003, 08:30 PM
That's a good point. If that would be easier to accomplish what I want, then yeah go ahead and show me how it'd work. It makes more sense to do it that way as well. But now, can you make it so, if the name entered is someone the user wants to sit with, it has maybe a bold style or something like that to distinguish who is registering as an adult, who is a child, and who is just a request-to-be-seated-next-to? Thanks!

arnyinc
01-14-2003, 08:45 PM
That can be accomplished by changing that italicize() function. This will italicize children's names, bold Adult names, and leave friend's name plain.



function italicize(gname, gtype){
if ((gtype.options[gtype.selectedIndex].text)=="Child"){
gname.style.fontStyle='italic';
gname.style.fontWeight='normal';
}
else if ((gtype.options[gtype.selectedIndex].text)=="Adult"){
gname.style.fontStyle='normal';
gname.style.fontWeight='bold';
}
else { //friend
gname.style.fontStyle='normal';
gname.style.fontWeight='normal';
}
}

noviceNate
01-14-2003, 09:12 PM
Instead of making the style change for the child, how about making it so that "(child)" would be entered at the end of the name entered? So, if it's an adult, nothing happens. If it's a child, add (child) at the end of the name, and if it's a friend, change it to all caps. ( using value.toUpperCase(); )

how about that??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum