user541597
user541597

Reputation: 4345

javascript function not working correctly

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>

Upvotes: 0

Views: 207

Answers (5)

falinsky
falinsky

Reputation: 7428

firstly delete the $ sign before firstclick in the else statement

Upvotes: 0

Skyrim
Skyrim

Reputation: 865

firstclick = true should be firstclick === true

Upvotes: 3

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5533

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

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175


function slidedown(){

    if (firstclick == true){


        $( '#rerooftext' ).slideDown(500);  

        alert(firstclick);
        firstclick = false;
    }

    else {
        $('#rerooftext').slideUp(500);
        firstclick = true;
    }
}


Upvotes: 0

Clay Garrett
Clay Garrett

Reputation: 1023

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.

Upvotes: 0

Related Questions