1

I'm trying to create a box that has two states:

(a) The box is a width of 70% (b) The box is a width of 10%

The user should be able to click the box to alternate between the two states, so if the box is in state 'a' then it will transition to state 'b' when clicked. If the box is in state 'b' it will transition to state 'a' when clicked and so on. I'm using the css below to set the initial state of the box:

#box1 {
position:relative;
left:0%;
top:0%;
width: 70%;
height: 100%;
background-color: #666;
z-index:4;
}

I'm using the javascript function below to allow the box to be clickable:

function slideLeft1(){
if(document.getElementById('box1').style.width='70%'){
document.getElementById('box1').style.width='10%';
 }
 else{
 document.getElementById('box1').style.width='70%';
 }
 }

It's working to go from 70% to 20% but not the reverse, so the if statement works but not the else statement. Any ideas??

1
  • 2
    if() needs ==, dunno if typo Commented Dec 15, 2012 at 4:34

1 Answer 1

2

You are not checking condition instead assigning it use double ==

function slideLeft1(){
if(document.getElementById('box1').style.width == '10%'){
document.getElementById('box1').style.width='70%';
 }
 else{
 document.getElementById('box1').style.width='10%';
 }
 }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! That worked! (I'm new to JS in case you couldn't tell).
Another question: For some reason I can only get this to work on the second time that I click on the box. The first time, nothing happens. I copied an pasted your code exactly. Any ideas?
Ya man it is because at first in div attribute it doesnt find anything named style tats why i just updated the code now.. to overcome your problem
can't find the updated code. did you update the code above? thanks!
just changed the 70% with 10% and vice versa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.