0

I have the following simple javascript function to slide an object up and down.

When I first click it should slide down which it should do and alerts with true which it should do. However on the second click I want it to slideup however it detects firstclick to be true again. Any ideas

<script type="text/javascript">
var firstclick = true;
function slidedown(){
 if (firstclick = true){
 $( '#rerooftext' ).slideDown(500); 
 alert(firstclick);
 firstclick = false;
 }
 else {
 $('#rerooftext').slideUp(500);
 $firstclick = true;
 }
}
</script>
asked Jan 17, 2012 at 5:15
1
  • check your browsers error console you will get your error Commented Jan 17, 2012 at 5:22

5 Answers 5

3

firstclick = true should be firstclick === true

gdoron
151k59 gold badges302 silver badges376 bronze badges
answered Jan 17, 2012 at 5:16
Sign up to request clarification or add additional context in comments.

2 Comments

also $firstclick = true; should be firstclick = true;
btw, you should checkout slideToggle() in jQuery
0

Where you have:

if (firstclick = true){

It should be:

if (firstclick == true){

You're setting firstclick to be true instead of testing if it is.

answered Jan 17, 2012 at 5:17

Comments

0

function slidedown(){
 if (firstclick == true){
 $( '#rerooftext' ).slideDown(500); 
 alert(firstclick);
 firstclick = false;
 }
 else {
 $('#rerooftext').slideUp(500);
 firstclick = true;
 }
}

answered Jan 17, 2012 at 5:18

Comments

0

The statement if (firstclick = true) is wrong in your code. This will return true always. Please change the statement to if(firstclick)

answered Jan 17, 2012 at 5:18

Comments

0

firstly delete the $ sign before firstclick in the else statement

answered Jan 17, 2012 at 5:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.