d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help Adding In Javascript
12Next
Add Reply New Topic New Poll
Member
Posts: 10,715
Joined: Sep 1 2007
Gold: 11,037.49
Nov 12 2015 06:52pm
Quote
<!DOCTYPE html>

<head><title>Vinny's New York Style Pizza</title>
<meta charset="utf-8" />


<style type="text/css">

body {margin-left:50px;
margin-right:50px;
font-size:16pt;
font-weight:bold;
}
h3 {border-style:solid;}

th,td {font-weight:bold;
font-size:14pt;
padding:0.50em;
border-style:solid;border-color:red;border-width:0.2em;
background-color:#E0E0E0;
}
caption {font-weight:bold;padding:0.5em;border-style:solid;
border-color:red;border-width:0.2em;background-color:lightblue;}
#outp {background-color:#FFFF66;width:20em;}
table {border-collapse:collapse;border-style:solid;border-color:red;border-width:0.2em;}
</style>

<script type="text/javascript" >















</script>
<body>
<form action="" method="post" >
<table>
<caption>Vinny's New York Style Pizza <br />
On-line Order Form </caption>
<tr>
<td colspan="3">First Name: &nbsp;
<input type="text" maxlength="10" name="fn" id="fn" />
(all alphabetics max length 10)
</td>
<th rowspan="10" id="outp"><h3>Order Details</h3><p id="outar"></p></th>
</tr>
<tr>
<td colspan="3">Phone #: &nbsp;
<input type="text" maxlength="8" name="ph" id="ph" />
&nbsp;&nbsp;Format: ddd-dddd
</td>
</tr>
<tr>
<th colspan="3">
SIZE: Select the size pizza you want:<br />
</th>
</tr>
<tr>
<td><input type="radio" name="size" id="small" value="10" checked="checked"/>Small - $10.00</td>
<td><input type="radio" name="size" id="med" value="12" />Medium - $12.00</td>
<td><input type="radio" name="size" id="large" value="16" />Large - $16.00</td>
</tr>
<tr>
<th colspan="3">
SAUCE: Select the sauce for your pizza:<br />
</th>
</tr>
<tr>
<td><input type="radio" name="sauce" id="marinara" value="marinara" checked="checked"/>Marinara </td>
<td><input type="radio" name="sauce" id="meat" value="meat" />Tomato with meat</td>
<td><input type="radio" name="sauce" id="white" value="white" />White</td>
</tr>
<tr>
<th colspan="3">
TOPPINGS: Select the toppings for your pizza
</th>
</tr>
<tr>
<td>
<input type="checkbox" id="pepperoni" value="pepperoni" />Pepperoni ($1.00)
</td>
<td>
<input type="checkbox" id="onions" value="onions" />Onions ($0.50)
</td>
<td>
<input type="checkbox" id="sausage" value="sausage" />Sausage ($1.00)
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="greenPeppers" value="greenPeppers" />Green Peppers ($0.50)
</td>
<td>
<input type="checkbox" id="olives" value="olives" />Olives ($0.50)
</td>
<td>
<input type="checkbox" id="extraCheese" value="extraCheese" />Extra Cheese ($1.00)
</td>
</tr>
<tr>
<th colspan="3">
<input type="button" value="Calculate Order Price" />
<input type="reset" value="Clear Form" />
</th>
</tr>
</table>

</form>
</body>
</html>


I have the table and buttons setup perfect. Now I need it to display what the user has selected. I'm not great with javascript and was looking where to start.

This post was edited by Zom8 on Nov 12 2015 06:52pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 12 2015 07:16pm
ok, starting place. you know you need to display what the user has selected. the trigger i assume is when the user clicks the button. that means you'll need an event listener. which event sounds relevant?

here's a small list:
Code
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page


an example of how to use it:

http://www.w3schools.com/js/js_events.asp
Member
Posts: 10,715
Joined: Sep 1 2007
Gold: 11,037.49
Nov 12 2015 08:24pm
Quote (carteblanche @ Nov 12 2015 08:16pm)
ok, starting place. you know you need to display what the user has selected. the trigger i assume is when the user clicks the button. that means you'll need an event listener. which event sounds relevant?

here's a small list:
Code
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page


an example of how to use it:

http://www.w3schools.com/js/js_events.asp


function totalcost(){
var type;
var newline= "";
";
var sum=0.00;
var toppings="";

if( document.getElementById("small").checked==true){
type="Small Pizza";
sum+=10.00;
}
if( document.getElementById("medium").checked==true){
type="Medium Pizza";
sum+=12.00;
}
if( document.getElementById("large").checked==true){
type="Large Pizza";
sum+=16.00;
}
if( document.getElementById("pepperoni").checked==true){
toppings=toppings+"Pepperoni, ";
sum+=1.00;
}
if( document.getElementById("olives").checked==true){
toppings=toppings+"Olives, ";
sum+=0.50;
}
if( document.getElementById("sausage").checked==true){
toppings=toppings+"Sausage, ";
sum+=1.00;
}
if( document.getElementById("peppers").checked==true){
toppings=toppings+"Peppers, ";
sum+=0.50;
}
if( document.getElementById("onions").checked==true){
toppings=toppings+"Onions, ";
sum+=0.50;
}

I got this using the getElementById function.. and I'm using an onclick button now but I don't know how to make it display my choices and price where <th rowspan="10" id="outp"><h3>Order Details</h3><p id="outar"></p></th> this is.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 12 2015 08:45pm
Quote (Zom8 @ Nov 12 2015 09:24pm)

I got this using the getElementById function.. and I'm using an onclick button now but I don't know how to make it display my choices and price where <th rowspan="10" id="outp"><h3>Order Details</h3><p id="outar"></p></th> this is.


http://www.w3schools.com/jsref/prop_html_innerhtml.asp
Member
Posts: 10,715
Joined: Sep 1 2007
Gold: 11,037.49
Nov 12 2015 08:51pm
Quote (carteblanche @ Nov 12 2015 08:16pm)
ok, starting place. you know you need to display what the user has selected. the trigger i assume is when the user clicks the button. that means you'll need an event listener. which event sounds relevant?

here's a small list:
Code
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page


an example of how to use it:

http://www.w3schools.com/js/js_events.asp


Quote
<!DOCTYPE html>
<html lang="en">
<!--
ITEC225 Assignment 5
This HTML document is an
order form for a fictional
New York Style Pizza Restaurant, template file -->

<head><title>Vinny's New York Style Pizza</title>
<meta charset="utf-8" />


<style type="text/css">

body {margin-left:50px;
margin-right:50px;
font-size:16pt;
font-weight:bold;
}
h3 {border-style:solid;}

th,td {font-weight:bold;
font-size:14pt;
padding:0.50em;
border-style:solid;border-color:red;border-width:0.2em;
background-color:#E0E0E0;
}
caption {font-weight:bold;padding:0.5em;border-style:solid;
border-color:red;border-width:0.2em;background-color:lightblue;}
#outp {background-color:#FFFF66;width:20em;}
table {border-collapse:collapse;border-style:solid;border-color:red;border-width:0.2em;}
</style>

<script type="text/javascript">

function totalcost(){
var type;
var newline= "";
";
var sum=0.00;
var toppings="";

if( document.getElementById("small").checked==true){
type="Small Pizza";
sum+=10.00;
}
if( document.getElementById("medium").checked==true){
type="Medium Pizza";
sum+=12.00;
}
if( document.getElementById("large").checked==true){
type="Large Pizza";
sum+=16.00;
}
if( document.getElementById("pepperoni").checked==true){
toppings=toppings+"Pepperoni, ";
sum+=1.00;
}
if( document.getElementById("olives").checked==true){
toppings=toppings+"Olives, ";
sum+=0.50;
}
if( document.getElementById("sausage").checked==true){
toppings=toppings+"Sausage, ";
sum+=1.00;
}
if( document.getElementById("peppers").checked==true){
toppings=toppings+"Peppers, ";
sum+=0.50;
}
if( document.getElementById("onions").checked==true){
toppings=toppings+"Onions, ";
sum+=0.50;
}

var length = toppings.length;
toppings = toppings.slice(0,length-2);

calculate(){
document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);
}

</script>
<body>
<form action="" method="post" >
<table>
<caption>Vinny's New York Style Pizza <br />
On-line Order Form </caption>
<tr>
<td colspan="3">First Name: &nbsp;
<input type="text" maxlength="10" name="fn" id="fn" />
(all alphabetics max length 10)
</td>
<th rowspan="10" class="outp"><h3>Order Details</h3><p id="opta">"Output"</p></th>
</tr>
<tr>
<td colspan="3">Phone #: &nbsp;
<input type="text" maxlength="8" name="ph" id="ph" />
&nbsp;&nbsp;Format: ddd-dddd
</td>
</tr>
<tr>
<th colspan="3">
SIZE: Select the size pizza you want:<br />
</th>
</tr>
<tr>
<td><input type="radio" name="size" id="small" value="10" checked="checked"/>Small - $10.00</td>
<td><input type="radio" name="size" id="med" value="12" />Medium - $12.00</td>
<td><input type="radio" name="size" id="large" value="16" />Large - $16.00</td>
</tr>
<tr>
<th colspan="3">
SAUCE: Select the sauce for your pizza:<br />
</th>
</tr>
<tr>
<td><input type="radio" name="sauce" id="marinara" value="marinara" checked="checked"/>Marinara </td>
<td><input type="radio" name="sauce" id="meat" value="meat" />Tomato with meat</td>
<td><input type="radio" name="sauce" id="white" value="white" />White</td>
</tr>
<tr>
<th colspan="3">
TOPPINGS: Select the toppings for your pizza
</th>
</tr>
<tr>
<td>
<input type="checkbox" id="pepperoni" value="pepperoni" />Pepperoni ($1.00)
</td>
<td>
<input type="checkbox" id="onions" value="onions" />Onions ($0.50)
</td>
<td>
<input type="checkbox" id="sausage" value="sausage" />Sausage ($1.00)
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="greenPeppers" value="greenPeppers" />Green Peppers ($0.50)
</td>
<td>
<input type="checkbox" id="olives" value="olives" />Olives ($0.50)
</td>
<td>
<input type="checkbox" id="extraCheese" value="extraCheese" />Extra Cheese ($1.00)
</td>
</tr>
<tr>
<th colspan="3">

<input type="button" onclick="calculate();" value="Price (Submit Button)"/>

<input type="reset" value="Clear Form"/>


</tr>
</table>

</form>
</body>
</html>


Already read your mind :D ... I've learned quite a bit just reading here the past few hours. I have that in there but I'm getting just this



Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 12 2015 08:55pm
Code
calculate(){
document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);
}


your syntax looks wrong. i assume that's supposed to be a separate function? needs the function keyword, and you need to close your other function
Member
Posts: 10,715
Joined: Sep 1 2007
Gold: 11,037.49
Nov 12 2015 08:57pm
Quote (carteblanche @ Nov 12 2015 09:55pm)
Code
calculate(){
document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);
}


your syntax looks wrong. i assume that's supposed to be a separate function? needs the function keyword, and you need to close your other function


Sorry I meant to delete it out. I included it in the first function. So take out the calculate() { and the orignal function closes

Quote
<script type="text/javascript">

function totalcost(){
var type;
var newline= "";
";
var sum=0.00;
var toppings="";

if( document.getElementById("small").checked==true){
type="Small Pizza";
sum+=10.00;
}
if( document.getElementById("medium").checked==true){
type="Medium Pizza";
sum+=12.00;
}
if( document.getElementById("large").checked==true){
type="Large Pizza";
sum+=16.00;
}
if( document.getElementById("pepperoni").checked==true){
toppings=toppings+"Pepperoni, ";
sum+=1.00;
}
if( document.getElementById("olives").checked==true){
toppings=toppings+"Olives, ";
sum+=0.50;
}
if( document.getElementById("sausage").checked==true){
toppings=toppings+"Sausage, ";
sum+=1.00;
}
if( document.getElementById("peppers").checked==true){
toppings=toppings+"Peppers, ";
sum+=0.50;
}
if( document.getElementById("onions").checked==true){
toppings=toppings+"Onions, ";
sum+=0.50;
}

var length = toppings.length;
toppings = toppings.slice(0,length-2);
{
document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);
}
</script>


This post was edited by Zom8 on Nov 12 2015 08:57pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 12 2015 09:10pm
i dont know what's wrong with your copy/paste, but it still looks wrong

like what's with the quote here?
Quote
function totalcost(){
var type;
var newline= "";
";
var sum=0.00;
var toppings="";


and you still have the brace above


Quote
var length = toppings.length;
toppings = toppings.slice(0,length-2);
{
document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);
}


and your onclick is still using calculate?

Quote
onclick="calculate();"


This post was edited by carteblanche on Nov 12 2015 09:11pm
Member
Posts: 10,715
Joined: Sep 1 2007
Gold: 11,037.49
Nov 12 2015 09:22pm
Quote
<!DOCTYPE html>
<html lang="en">
<!--
ITEC225 Assignment 5
This HTML document is an
order form for a fictional
New York Style Pizza Restaurant, template file -->

<head><title>Vinny's New York Style Pizza</title>
<meta charset="utf-8" />


<style type="text/css">

body {margin-left:50px;
margin-right:50px;
font-size:16pt;
font-weight:bold;
}
h3 {border-style:solid;}

th,td {font-weight:bold;
font-size:14pt;
padding:0.50em;
border-style:solid;border-color:red;border-width:0.2em;
background-color:#E0E0E0;
}
caption {font-weight:bold;padding:0.5em;border-style:solid;
border-color:red;border-width:0.2em;background-color:lightblue;}
#outp {background-color:#FFFF66;width:20em;}
table {border-collapse:collapse;border-style:solid;border-color:red;border-width:0.2em;}
</style>

<script type="text/javascript">

function totalcost(){
var type;
var newline= "";
var sum=0.00;
var toppings;

if( document.getElementById("small").checked==true){
type="Small Pizza";
sum+=10.00;
}
if( document.getElementById("medium").checked==true){
type="Medium Pizza";
sum+=12.00;
}
if( document.getElementById("large").checked==true){
type="Large Pizza";
sum+=16.00;
}
if( document.getElementById("pepperoni").checked==true){
toppings="Pepperoni, ";
sum+=1.00;
}
if( document.getElementById("olives").checked==true){
toppings="Olives, ";
sum+=0.50;
}
if( document.getElementById("sausage").checked==true){
toppings="Sausage, ";
sum+=1.00;
}
if( document.getElementById("peppers").checked==true){
toppings="Peppers, ";
sum+=0.50;
}
if( document.getElementById("onions").checked==true){
toppings="Onions, ";
sum+=0.50;
}

var length = toppings.length;{
toppings = toppings.slice(0,length-2);}

document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);
}

</script>
<body>
<form action="" method="post">
<table>
<caption>Vinny's New York Style Pizza <br />
On-line Order Form </caption>
<tr>
<td colspan="3">First Name: &nbsp;
<input type="text" maxlength="10" name="fn" id="fn" />
(all alphabetics max length 10)
</td>
<th rowspan="10" class="outp"><h3>Order Details</h3><p id="opta"></p></th>
</tr>
<tr>
<td colspan="3">Phone #: &nbsp;
<input type="text" maxlength="8" name="ph" id="ph" />
&nbsp;&nbsp;Format: ddd-dddd
</td>
</tr>
<tr>
<th colspan="3">
SIZE: Select the size pizza you want:<br />
</th>
</tr>
<tr>
<td><input type="radio" name="size" id="small" value="10" checked="checked"/>Small - $10.00</td>
<td><input type="radio" name="size" id="med" value="12" />Medium - $12.00</td>
<td><input type="radio" name="size" id="large" value="16" />Large - $16.00</td>
</tr>
<tr>
<th colspan="3">
SAUCE: Select the sauce for your pizza:<br />
</th>
</tr>
<tr>
<td><input type="radio" name="sauce" id="marinara" value="marinara" checked="checked"/>Marinara </td>
<td><input type="radio" name="sauce" id="meat" value="meat" />Tomato with meat</td>
<td><input type="radio" name="sauce" id="white" value="white" />White</td>
</tr>
<tr>
<th colspan="3">
TOPPINGS: Select the toppings for your pizza
</th>
</tr>
<tr>
<td>
<input type="checkbox" id="pepperoni" value="pepperoni" />Pepperoni ($1.00)
</td>
<td>
<input type="checkbox" id="onions" value="onions" />Onions ($0.50)
</td>
<td>
<input type="checkbox" id="sausage" value="sausage" />Sausage ($1.00)
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="greenPeppers" value="greenPeppers" />Green Peppers ($0.50)
</td>
<td>
<input type="checkbox" id="olives" value="olives" />Olives ($0.50)
</td>
<td>
<input type="checkbox" id="extraCheese" value="extraCheese" />Extra Cheese ($1.00)
</td>
</tr>
<tr>
<th colspan="3">

<input type="button" onclick="totalcost();" value="Price (Submit Button)"/>

<input type="reset" value="Clear Form"/>


</tr>
</table>

</form>
</body>
</html>


Okay cleaned it up I think, but If I've thought this through right my method of getting the "opta" to post topping choices underneath Order Details is wrong.

This post was edited by Zom8 on Nov 12 2015 09:24pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 12 2015 09:25pm
still have problems...

Quote
document.getElementById("opta").innerHTML = type+newline+toppings"+newline+"toppings"+newline+"Price - $"+sum.toFixed(2);


what is that first quote doing right there after toppings? you should be seeing errors in the console. you dont need me to point this out

This post was edited by carteblanche on Nov 12 2015 09:26pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll