1 Replies - 772 Views - Last Post: 06 February 2012 - 02:22 PM

#1 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

simple if/else-if not working

Posted 06 February 2012 - 02:12 PM

I am trying to basically make a toggle button on the first button click i need it to move down on the second button click move back up to the original position. I am simply just trying to make the button have a state by changing the class. So first click it removes the up class and adds the down class. On the second click it removes the down class and adds the up class. for some reason the it removes the up class and adds the down but it will not do the opposite
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/css; charset=utf-8" />
<title>testing document</title>

<link href="css2.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$("button").click(function(){
if($("button").hasClass("up"))
{
$("button").removeClass("up")
$("button").addClass("down")
}
else if($("button").hasClass("up"))
{
$("button").removeClass("down")
$("button").addClass("up")
};
});




});
</script>

</head>

<body>
<button class= "down" >Show all p elements</button>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: simple if/else-if not working

#2 LaughingBelly  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 103
  • Joined: 11-April 11

Re: simple if/else-if not working

Posted 06 February 2012 - 02:22 PM

else if($("button").hasClass("up"))

You are checking to see if you have "up" class in the else - you already did that in the if part. Else part needs to check for "down"
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1