0

I have two pieces of code, first of all I have my code which toggles open a div with an included close button:

http://jsfiddle.net/spadez/uhEgG/27/

$(document).ready(function () {


    $('#country').click(function () {
        $("#country_slide").slideToggle();
    });

    $('#close').click(function (e) {
        e.preventDefault();
        $('#country_slide').slideToggle();
    });

});

Then I also have my Ajax code which is designed to fire when the div has been opened:

$(function () {
    $('#country_link').on('click', function () {
        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: '/ajax/test.html',
            timeout: 5000,
            beforeSend: function () {
                $('.loader').show();
            },
            success: function (data, textStatus) {
                $("#country_slide").html(data);
                alert('request successful');
            },
            error: function (xhr, textStatus, errorThrown) {
//                $("#country_slide").hide('fast');
//                alert('request failed');                
            },
            complete: function () {
                $('.loader').hide();
            },
        });
        return false;
    });
});

What I am stuck with now is, how do I make the ajax only execute when the div is being opened? Because I am working with a toggle and close button it seems difficult to work out what the click is doing, whether it is opening it or closing it.

I guess my options are to have some kind of flag or alternatively have some "if" code, so if class is equal to .hidden then do not execute. I haven't been able to integrate either of these solutions and I am unsure if either of them is the proper way to achieve this.

2
  • What exactly is the issue you're having? Can you post a little HTML? Is #country_link inside of #country, or no? If it's not, it would make sense to either have a variable that declares whether it's available for use. But if you have a link that doesn't do something sometimes, you should indicate that to the user somehow. Commented Jun 6, 2013 at 14:07
  • 1
    You could bind data to the element (e.g. loaded) to the div when it's loaded, so next time you click on the element it doesn't has to load again. Commented Jun 6, 2013 at 14:19

3 Answers 3

1

Include the check as part of your slide function:

$("#country_slide").slideToggle(function() {
    if ($(this).is(":visible")) {
        alert("im visible!");
    }
});

Demo: http://jsfiddle.net/tymeJV/uhEgG/28/

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

4 Comments

This will run the ajax request every time you open the div.
Yes I can see this problem happening
Thought this was what you wanted... "how do I make the ajax only execute when the div is being opened?"
Indeed, that's why I didn't downvote, but also don't want to upvote because requesting the same page over and over again seems useless to me.
1
if($("#country_slide").is(":visible")) 
  //call ajax    

Comments

1

This code adds data to the element, to check if it's already loaded next time you click on it.

Currently I am not able to test it, so it may contain errors.

$(function () {
    $('#country_link').on('click', function (e) {
        // Prevent from following the link, if there is some sort of error in
        // the code before 'return false' it would still follow the link.
        e.preventDefault();

        // Get $link because 'this' is something else in the ajax request.
        var $link = $(this);
        // Exit if the data is loaded already
        if ($link.data('loaded') === true)
            return false;

        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: '/ajax/test.html',
            timeout: 5000,
            beforeSend: function () {
                $('.loader').show();
            },
            success: function (data, textStatus) {
                $("#country_slide").html(data);
                alert('request successful');
                // If successful, bind 'loaded' in the data
                $link.data('loaded', true)
            },
            error: function (xhr, textStatus, errorThrown) {
//                $("#country_slide").hide('fast');
//                alert('request failed');
            },
            complete: function () {
                $('.loader').hide();
            },
        });
    });
});

4 Comments

Thank you for the help. This is your code in action but when you click the link nothing happens and nothing comes up in console either, so I can;t figure out where it's getting stuck jsfiddle.net/spadez/uhEgG/34
Ah, you're welcome. That fiddle has no element with class loader, and I also noticed I had something wrong in the code. !== true should be === true, because it SHOULD exit when it's true. Hope this helps.
So how exactly does this work, it loads it up for the first time, but then since it is already loaded, it won't then reload it next time it will just pull it from the cache?
@Jimmy The content will still be inside the div, it isn't removed. If you plan to use the same div for multiple ajax requests you couls store the results in an array.

Your Answer

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.