tags:

views:

81

answers:

1

Not sure if this is possible but can you load and unload into a div on a jquery toggle?

something a bit like this?

$("#IDOFCLICK").live('click',function(){
$(this).toggleClass("active").('#IDOFDIVTOLOAD').load('PAAGETOLOAD').slideToggle("slow");
});

if you can I guess the above is not right, but, how would you also unload on the "reverse" toggle?

+1  A: 

That won't work. The toggle you're using just toggles the class. There is a toggle event you could use, but it is not supported by live() to my knowledge.

When you say unload I assume you want to empty the content of #IDOFDIVTOLOAD. If that's right, you could try this:

$("#IDOFCLICK").live('click',function(){
    $(this).toggleClass("active");
    var $loadElement = $('#IDOFDIVTOLOAD');
    if( $loadElement.is(':empty') ) {
        $loadElement.load('PAAGETOLOAD').slideToggle("slow");
    } else {
        $loadElement.empty().slideToggle("slow");
    }
});

jQuery docs:

patrick dw
Thanks Patrick will give it a go, I knew mine was totally wrong but I wasn't even sure if it could be done.
Sounds good. Just a note, the `.is()` won't be right if there was some other content inside `#IDOFDIVTOLOAD` before the load. If that's the case, you'll need to change the test in the `if()`.
patrick dw