Masood Ahmed
Masood Ahmed

Reputation: 25

How to use javascript conditions correctly?

I have a slider function, which works perfectly fine with moveRight() but I am stuck, and can use it only once.

I decided to use a condition to disable the move function and change the attributes of links on the second click. My code below:

$('#control_next').click(function () {
    var used = 0;
    if (used == 0) {
        moveRight();
        used = 1;
    } else if (used == 1) {
        $('.control_next').attr('href', '#business-events');
        $('.control_next').addClass( "url" );
    }  
    alert(used);
});

Upvotes: 0

Views: 49

Answers (2)

Udhaya Kumar V
Udhaya Kumar V

Reputation: 83

You should declare variable Globally. Like

var used = 0;
$('#control_next').click(function () {
     if (used == 0) {
        moveRight();
        used = 1;
     }
     else if (used == 1) {
        $('.control_next').attr('href', '#business-events');
        $('.control_next').addClass( "url" );
     }  
});

Upvotes: 1

Mivaweb
Mivaweb

Reputation: 5712

You have to place the varaible used outside your event handler because else it will always be 0 with every click:

var used = 0;

$('#control_next').click(function () {
     if (used == 0) {
        moveRight();
        used = 1;
     }
     else if (used == 1) {
        $('.control_next').attr('href', '#business-events');
        $('.control_next').addClass( "url" );
     }  
     alert(used);
});

Upvotes: 1

Related Questions