Fiach Oneill
Fiach Oneill

Reputation: 13

What's wrong with this script and why am I getting no error?

So I'm relatively new to JS and decided to practice a bit. I wanted to make a webpage to select how many products you want to buy, with two buttons a plus and a minus. The program was supposed to add 1 to an input or take 1 depending on what button was impressed. image of webpage

So that worked fine but then I added an if statement to make sure the user didn't enter above 4 tickets or lower than 0 tickets. This produced nothing and in developers tools on chrome it just flashes an error but doesn't keep it there. Any help would be appreciated.

<!DOCTYPE html>
<html>
<head>
	<style>
	.myButton {
		background-color:#44c767;
		-moz-border-radius:28px;
		-webkit-border-radius:28px;
		border-radius:28px;
		border:1px solid #18ab29;
		display:inline-block;
		cursor:pointer;
		color:#ffffff;
		font-family:Arial;
		font-size:17px;
		padding:16px 31px;
		text-decoration:none;
		text-shadow:0px 1px 0px #2f6627;
	}
	.myButton:hover {
		background-color:#5cbf2a;
	}
	.myButton:active {
		position:relative;
		top:1px;
	}

	</style>
	<script>

	function plus(){
	document.getElementById("htop").value=Number(document.getElementById("htop").value)+1;

	}
	function minus(){
	document.getElementById("htop").value=Number(document.getElementById("htop").value)-1;

	}
	function validate(){
		if(Number(document.getElementById("htop").value)<=0){
			alert("You can not submit less than NULL tickets");
			return false;
		}
		else if(Number(document.getElementById("htop").value)>4){
		alert("you can not buy more than 4 tickets at once");
		return false;
		}
		else{
			alert("you may proceed");
			return true;
		}
	}

	</script>
</head>
<body >
	<form>
	<button id="plus" class="myButton" onclick="plus()">+</button>
	<button id="minus" class="myButton" onclick="minus()">-</button>
	<input type="text" id="htop" value="0" />
	<br>
	<input type="submit" value="Submit" onclick="validate()">
</form>

</body>
</html>

Upvotes: 0

Views: 46

Answers (1)

Neha
Neha

Reputation: 19

I believe that with what you are trying to do, you should not use form tags. Try removing those.

<body >
    <button id="plus" class="myButton" onclick="plus()">+</button>
    <button id="minus" class="myButton" onclick="minus()">-</button>
    <input type="text" id="htop" value="0" />
    <br>
    <input type="submit" value="Submit" onclick="validate()">


</body>

Upvotes: 1

Related Questions