Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello folks.
I've come a cropper with a javascript if else statement. I know what I want to achieve with the javascript syntax which is almost identical to actionscript which I have better knowledge of. So...

Dom - declaring var - btnone
XML
<script type="text/javascript">
    $(document).ready(function(){
    var btnone = true;
    });
</script>


Function - The if / else statement fails to get called as you'll see alert("no")fires but alert("yes")doesn't.

XML
<script type="text/javascript">
$('#a').live('click', function() {
        alert("no");
        if(btnone == "true") {
        alert("yes");
        $('#one').animate({top: '+=50',}, 500, function() {
        $('#one').animate({top: '-=50'}, 1000 );
        });
        } else
        if(btnone == "false") {
        //**var btntwo = false;
        $('.9, .8').hide();
        }
});
</script>


P.S - I also would like to control other var true or false values like so...refer to **. Correct method?
Any ideas? Many Thanks.
Posted
Updated 2-Sep-11 1:57am
v2

You declared btnone in an anonymous function, which means it is not available in the scope of your event handler. Try moving the declaration out of the jQuery ready function:
XML
<script type="text/javascript">
    // global variable
    var btnone = true;

    $(document).ready(function(){
        // jQuery ready function - do DOM stuff here
    });
</script>
 
Share this answer
 
Comments
Dylan Morley 2-Sep-11 11:09am    
Comment from OP:

Nice one Graham. Problem solved!
Thank you for your explanation and assistance.
Jim
You've set the variable to a boolean, but you're comparing it to a string

if(btnone == "true")


try this
if(btnone == true)
 
Share this answer
 
Comments
Dylan Morley 2-Sep-11 9:32am    
Comment from OP:

Hello Dylan
Good idea but no change, now I get an error in ff - btnone is not defined. Any clues?
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900