Kyriakos Hadjipandeli
Kyriakos Hadjipandeli

Reputation: 25

Simple js calculator with radio tag

I found subjects like mine but I could not fix my problem. So I wanted to calculate the cost of tickets form radio tags by multiplying the price with the amount of tickets using if statements for each option without jquery. I can not figure it out why I do not have an output. It looks like none of my functions work and I can not find why.

Can you help me please, I know it is easy but I am still a beginner

<!doctype html>
<html>
<head>
	<title>No Boundaries</title>
	<link rel="stylesheet" href="styles1.css">
	<script type="text/javascript">
		function validate(){
			if(!document.form1.cond.checked)
				{alert("Please accept the Terms and Conditions");
				return false;
			}
			if(document.form1.name.value.length < 2)
				{alert(“Please enter your full name, not just an initial”);
				return false;
			}
			return true;
		}
		function cost() {
			if (document.form1.accom["v"].checked) {
				var amount = parseInt(document.form1.amount.value);
				var ans = 90 * amount;
				document.form1.total1.value = ans;
			} 
			else if (document.form1.accom["t"].checked) {
				var amount = parseInt(document.form1.amount.value);
				var ans = 150 * amount;
				document.form1.total1.value = ans;
			} 
			else if (document.form1.accom["c"].checked) {
				var amount = parseInt(document.form1.amount.value);
				var ans = 45 * amount;
				document.form1.total1.value = ans;
			}
		}
		function post(){
			if(document.form1.del["1"].checked){
				var num = 0;
				var ans = num;
				document.form1.total2.value = ans;
			}
			if(document.form1.del["2"].checked){
				var num = 12;
				var ans = num;
				document.form1.total2.value = ans;
			}
			if(document.form1.del["3"].checked){
				var num = 16;
				var ans = num;
				document.form1.total2.value = ans;
			}
			if(document.form1.del["4"].checked){
				var num = 20;
				var ans = num;
				document.form1.total2.value = ans;
			}
		}
		function damage(){
			var total1 = parseInt(document.form1.total1.value);
			var total1 = parseInt(document.form1.total1.value);
			var ans = total1 + total2;
			document.form.total3.value = ans;
		}
	</script>

</head>

<body>
<section>
  <form name="form1">
		<fieldset>
			<legend>Personal details</legend>
			<div>
				Please enter your full name<br>
				<input name="name" type="text" required onsubmit="return validate();"><br>
			</div>
			<div>
				Address<br>
				<input name="addresss" type="text" required><br>
				<input type="text"><br>
				<input type="text"><br>
				<input type="text"><br>
			</div>
			<div>
				Phone Number<br>
				<input name="phone" type="tel"><br>
			</div>
			<div>
				Email Address<br>
				<input name="email" type="email" required><br>
			</div>
			<div>
				Date of Birth<br>
				<input name="birth" type="date"><br>
			</div>
		</fieldset>
		<fieldset>
			<legend>Ticket Details</legend>
				<div>
					Type of T<br>
					<label for="vil">V</label>
					<input type="radio" name="accom[]" value="v">
					<label for="town">T</label>
					<input type="radio" name="accom[]" value="t">
					<label for="con">C</label>
					<input type="radio" name="accom[]" value="c">
				</div>
				<div>
					Quantity of T
					<input name="amount" type="number" min="1" max="10" required><br>
					<br>
					<input type="button" name="cost" value="C C" onclick="cost();"><br>
				</div>
				<div>
					Delivery Options<br>
					<input type="radio" name="del[]" value="1" >Free<br>
					<input type="radio" name="del[]" value="2" >£12<br>
					<input type="radio" name="del[]" value="3" >£16<br>
					<input type="radio" name="del[]" value="4" >£20<br>
					<br>
					<input type="button" value="C D" onclick="post();"><br>
				</div>
		</fieldset>
		<fieldset>
			<legend>Terms And Conditions</legend>
			<input name="cond" type="checkbox" onsubmit="return validate();">
		</fieldset>
		<fieldset>
			<legend>Cost</legend>
			<input type="text" name="total1" readonly><br>
			<input type="text" name="total2" readonly><br>
			<input type="text" name="total3" readonly><br>
			<br>
			<input type="button" value="C F C" onclick="damage();"><br>
		</fieldset>
		<br><br>
		<fieldset>
			<input type="submit">
		</fieldset>
  </form>
</section>
</body>

</html>

Upvotes: 0

Views: 141

Answers (2)

DavideDM
DavideDM

Reputation: 1495

Ok I found your errors. This is the code for html

<div>
   Type of T<br>
   <label for="vil">V</label>
   <input type="radio" name="accom" value="v">
   <label for="town">T</label>
   <input type="radio" name="accom" value="t">
    <label for="con">C</label>
</div>
<div>
   Quantity of T
   <input name="amount" type="number" min="1" max="10" required><br>
   <br>
   <input type="button" name="cost" value="C C" onclick="costs();"><br>
 </div>

Next, in your javascript you have tho change the name of function; instead of cost you have to renaming in costs

 function costs() {
    if (document.form1.accom.value == 'v') {
        var amount = parseInt(document.form1.amount.value);
        var ans = 90 * amount;
        document.form1.total1.value = ans;
    } 
    else if (document.form1.accom.value == 't') {
        var amount = parseInt(document.form1.amount.value);
        var ans = 150 * amount;
        document.form1.total1.value = ans;
    } 
    else if (document.form1.a`enter code here`ccom.value == 'c') {
        var amount = parseInt(document.form1.amount.value);
        var ans = 45 * amount;
        document.form1.total1.value = ans;
    }
}

That's all

Upvotes: 0

Ayman El Temsahi
Ayman El Temsahi

Reputation: 2610

the cost function should be something like this,

function cost() {
    if (document.form1.accom.value.toLowerCase() == "v") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 90 * amount;
        document.form1.total1.value = ans;
    } else if (document.form1.accom.value.toLowerCase() == "t") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 150 * amount;
        document.form1.total1.value = ans;
    } else if (document.form1.accom.value.toLowerCase() == "c") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 45 * amount;
        document.form1.total1.value = ans;
    }
}

And to make the code more refactored, make it like this,

function cost() {
    var val = document.form1.accom.value.toLowerCase();
    var amount = parseInt(document.form1.amount.value);
    var ans;
    if (val == "v") {
        ans = 90 * amount;
    } else if (val == "t") {
        ans = 150 * amount;
    } else if (val == "c") {
        ans = 45 * amount;
    }
    document.form1.total1.value = ans;
}

Upvotes: 1

Related Questions