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

checkbox js not working 1

Status
Not open for further replies.

TheCandyman

Technical User
Sep 9, 2002
761
US
I have a page where 10 people can enter their name and click one of two possible checkbox for each person(adult or child). I think i have this correct, but i just can't get any part of it to work. What i want is when a checkbox is clicked, it checks that a name is entered in the textbox and if so then loop through each name box to figure up a total price for display.


In <Head>
Code:
function displ()
{
	temp=0;
	    for (Count = 1; Count < 11; Count++) {
			if (document.form1.Name[Count].value != "") {
				alert (document.form1.Name[Count]);
				if (form.Adult[Count].checked == 1) {temp=temp + parseInt(30)}	
				if (form.Adult[Count].checked == 1) {temp=temp + parseInt(15)}
				alert (temp);
			}
    	}	
    document.form1.Amount.value = temp ;
  return true;
}



Form:
Code:
<form action="CC.asp" method="post" name="form1" class="MyForm">
  <input type="text" size="30" name="Name1" value="" onclick="return displ();" class="text" />
  <input name="Adult1" type="checkbox" value="" onclick="return displ();" />
  <input name="Child1" type="checkbox" value="" onclick="return displ();" />
...
...
...
 
Hi

JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]displ[/color][teal]()[/teal]
[teal]{[/teal]
  temp[teal]=[/teal][purple]0[/purple][teal];[/teal]
  [b]for[/b] [teal]([/teal]Count [teal]=[/teal] [purple]1[/purple][teal];[/teal] Count [teal]<[/teal] [purple]2[/purple][teal];[/teal] Count[teal]++)[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal]document[teal].[/teal]form1[teal].[/teal][highlight]elements[/highlight][teal][[/teal][green][i][highlight]'[/highlight]Name[highlight]'[/highlight][/i][/green][teal][highlight]+[/highlight][/teal]Count[teal]].[/teal]value [teal]!=[/teal] [green][i]""[/i][/green][teal])[/teal] [teal]{[/teal]
      [COLOR=darkgoldenrod]alert[/color] [teal]([/teal]document[teal].[/teal]form1[teal].[/teal][highlight]elements[/highlight][teal][[/teal][green][i][highlight]'[/highlight]Name[highlight]'[/highlight][/i][/green][teal][highlight]+[/highlight][/teal]Count[teal]]);[/teal]
      [b]if[/b] [teal]([/teal]document[teal].[/teal]form1[teal].[/teal][highlight]elements[/highlight][teal][[/teal][green][i][highlight]'[/highlight]Adult[highlight]'[/highlight][/i][/green][teal][highlight]+[/highlight][/teal]Count[teal]].[/teal]checked[teal])[/teal] temp[teal]=[/teal]temp [teal]+[/teal] [COLOR=darkgoldenrod]parseInt[/color][teal]([/teal][purple]30[/purple][teal])[/teal]
      [b]if[/b] [teal]([/teal]document[teal].[/teal]form1[teal].[/teal][highlight]elements[/highlight][teal][[/teal][green][i][highlight]'[/highlight]Child[highlight]'[/highlight][/i][/green][teal][highlight]+[/highlight][/teal]Count[teal]].[/teal]checked[teal])[/teal] temp[teal]=[/teal]temp [teal]+[/teal] [COLOR=darkgoldenrod]parseInt[/color][teal]([/teal][purple]15[/purple][teal])[/teal]
      [COLOR=darkgoldenrod]alert[/color] [teal]([/teal]temp[teal]);[/teal]
    [teal]}[/teal]
  [teal]}[/teal]    
  document[teal].[/teal]form1[teal].[/teal]Amount[teal].[/teal]value [teal]=[/teal] temp [teal];[/teal]
  [b]return[/b] [b]true[/b][teal];[/teal]
[teal]}[/teal]

Feherke.
 
feherke,

i didn't even think about the 'elements' way. I changed the upper count to 11 so it loops through all 10 input boxes and it works great! Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top