Jump to content

Help with Javascript

CoolUserName
Go to solution Solved by fizzlesticks,

You're changing the height of the box but you're comparing the width of the box. Change curr_width to curr_height and read back the height instead.

 

edit: Also "=" is used to set a variable, you need to use == or === to compare values.

I am trying to make a button that will make a div get bigger and smaller. So if the div has a height of 100px than it will become 300px tall when the button is pressed, and then if it is 300px tall then when you press the button it will become 100px tall. this is my code currently:

<html>	<head>		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>		<script>			function addWidth() {				var box= document.getElementById("box");					var curr_width = parseInt(box.style.width);				if(curr_width = 300){					$("#box").animate({height:"100px"});				}				else if(curr_width = 100){              					$("#box").animate({height:"300px"});				}			}		</script>	</head>	<body>		<div id="box" style="background:#98bf21; height:300px; width:100px; margin:6px;">		       <input type="button" value="Click Me" onclick="addWidth()" />	        </div>	</body></html>
Link to comment
Share on other sites

Link to post
Share on other sites

You're changing the height of the box but you're comparing the width of the box. Change curr_width to curr_height and read back the height instead.

 

edit: Also "=" is used to set a variable, you need to use == or === to compare values.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×