1

When a div is opnened i want to load html content into it via ajax. This is the code im working with:

http://jsfiddle.net/uhEgG/2/

$(document).ready(function () {


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

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

});

The code I think I need is this:

    $.ajaxSetup ({  
        cache: false  
    });  
    var ajax_load = "Loading...";
    var loadUrl = "www.test.com/site.html";  
    $("#load_basic").click(function(){  
        $("#country_slide").html(ajax_load).load(loadUrl);  
    })

How can I make it work to make it load up when the div is opened by the code above, firstly because it is setup for a click function not a toggle function, and second, because the toggle doesn't seem to be able to distinguish if the div is open or not.

3 Answers 3

1

to make it load up when the div is opened by the code above

$("#country_slide").slideToggle(function(){
  if($(this).is(':visible')){
    $("#country_slide").html(ajax_load).load(loadUrl);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Also Probably Close needs to have the event delegated as countryslide's content is updated... and close is with in that.
1

Try to delegate the events.. Looks like the element is not yet available in the DOm when the event is bound

Replace

$('#country').click(function () {

with

$(staticContainer).on('click', '#country', function () {

staticContainer is the element which is already in your DOM when the event is bound and the ancestor of country

Comments

1

Either store the slide state in a variable or in a data attribute liek this:

<div id="country_slide" data-state="1">

And make something like this:

$('#country').click(function () {
        $("#country_slide").slideToggle();
        if ($("#country_slide").attr("data-state") == 0)
            $("#country_slide").html(ajax_load).load(loadUrl); 
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.