1

I would like to know if it is possible to pass a variable from inside an 'If', 'Else If' or 'Else' statement to another section without the need of PHP, just using JQuery.


JQuery Code:

$('#Info-Side-Tabs a').click(function() {
    var clicked = $(this).attr('href');
    if (clicked == "#Map-Selection-Info-Close") {
        $('#Info-Side-Tabs li').removeClass('active');
        $(this).parent().addClass('active');
        $(this).attr("href","#Map-Selection-Info-Open");
        $(this).text('Open Me');
        $('#Map-Selection-Info').animate({marginLeft: '978px'}, 1000);
        $('#ATWI80C-Map').animate({width: '900px' }, 1000);
    } else if (clicked == "#Map-Selection-Info-Open") {
        $(this).parent().removeClass('active');
        $(this).attr("href","#Map-Selection-Info-Close");
        $(this).text('Close Me');
        $('#Map-Selection-Info').animate({marginLeft: '670px'}, 1000);
        $('#ATWI80C-Map').animate({width: '600px' }, 1000);
    } else {
        $('#Info-Side-Tabs li').removeClass('active');
        $(this).parent().addClass('active');
        var activeTab = $(this).attr('href');
        $('#tab-content > div:visible').hide();
        $(activeTab).show();
        return false;
    }
});

In the above, it will close or open my side information with tabs on. if clicked does not equal "#Map-Selection-Info-Close", I need to set a variable of the name. The reason why I would like to do this is that #Map-Selection-Info-Close/#Map-Selection-Info-Open is on the tab switcher, however there is no content for them so it keeps the previous content but switches the class to the active one upon closing. Upon opening, you can see the previous content, however I do not know how to set the active class back to the current content of which is shown.

I'm pretty new to JQuery and this is the best thing I have done so far and I think I am picking up everything so easily now that I have clicked, however I do not know how to, if possible, to do the above and I thank you for any help and/advice in doing what I wish to achieve.

Best Regards,

Tim

1 Answer 1

1

I kept on searching for the answer, reading on article and learning and using new key words to define new search results. I learnt that I first need to make a variable global;

// To set fully global for all your functions, set it here
var lastclicked;
$('#Info-Side-Tabs a').click(function() {
    var clicked = $(this).attr('href');
    if (clicked == "#Map-Selection-Info-Close") {
        // functions
    } else if (clicked == "#Map-Selection-Info-Open") {
        // functions
    } else {
        // functions
    }
});

So I set my new variable 'lastclicked' with no value. The next step is a key value to be able to use this global variable. If you would like to set this global variable anywhere, you much leave out the 'var', example;

$('#Info-Side-Tabs a').click(function() {
    var lastclicked;
    var clicked = $(this).attr('href');
    if (clicked == "#Map-Selection-Info-Close") {
        // functions 
    } else if (clicked == "#Map-Selection-Info-Open") {
        // functions
    } else {
        lastclicked = clicked;
    }
    alert(lastclicked);
});

I hope this helps with anyone else trying to find out a similar solution.

Best Regards,

Tim

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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