views:

545

answers:

3

Dear all,

I have made an custom collapsible fieldset control in asp.net. I use jquery to add the toggle effects. The control works perfectly but when i am using my fieldsets inside an updatepanel, afer a postback i loose my jquery logic because of the document.ready.

Now i have read about the new Live() function of Jquery but i don't get it working. What do i do wrong? Has someone the answer??

Thanks a lot

My Jquery code is:

$(document).ready(function() {

    $.fn.collapse = function(options) {
        var defaults = { closed: false }
        settings = $.extend({}, defaults, options);

        return this.each(function() {
            var obj = $(this);

            obj.find("legend").addClass('SmartFieldSetCollapsible').live("click", function() {


                if (obj.hasClass('collapsed')) { 
                obj.removeClass('collapsed').addClass('SmartFieldSetCollapsible'); }

                $(this).removeClass('collapsed');

                obj.children().next().toggle("slow", function() {

                    if ($(this).is(":visible")) {

                        obj.find("legend").addClass('SmartFieldSetCollapsible');
                        obj.removeAttr("style");
                        obj.css({ padding: '10px' });
                        obj.find(".imgCollapse").css({ display: 'none' });
                        obj.find(".imgExpand").css({ display: 'inline' });

                    }
                    else {

                        obj.css({ borderLeftColor: 'transparent', borderRightColor: 'transparent', borderBottomColor: 'transparent', borderWidth: '1px 0px 0px 0px', paddingBottom: '0px' });
                        obj.find(".imgExpand").css({ display: 'none' });
                        obj.find(".imgCollapse").css({ display: 'inline' });

                    }
                });
            });

            if (settings.closed) {
                obj.addClass('collapsed').find("legend").addClass('collapsed');
                obj.children().filter("p,img,table,ul,div,span,h1,h2,h3,h4,h5").css('display', 'none');
            }
        });
    };


});


$(document).ready(function() {

$("fieldset.SmartFieldSetCollapsible").collapse();

});
+2  A: 
            if (obj.hasClass('collapsed')) { 
            obj.removeClass('collapsed').addClass('SmartFieldSetCollapsible'); }

            $(this).removeClass('collapsed');

First you want to remove the class an add another class if it has the class collapsed, an then you remove the class collapsed. I don't know if it affects the working of the system but it is worth to try.

Does the function work if you just use .click (when the field aren't updated)?

dododedodonl
Hi all,The live( doesn't get called. So this woun't work. I have solved this issue by using a hidden field. In this field i keep track of which fielset is open or closed. So now everything works fine. So thank u all for your replay'sGreatings Talsja
Talsja
+2  A: 

The problem is that you are doing more then just a plain selector for your live selection

From api.jquery.com "DOM traversal methods are not fully supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selecton"

PetersenDidIt
+1 Thanks, I always had problems with the `live()` method, that s a good thing to know!
adardesign
It makes sense if you think about it... `live` is supposed to check whether target elements match a particular selector at event time, not at call time. A `$()` wrapper at heart represents a list of elements matched at the time it was created; it's a total hack that you can call `live()` on it to repurpose the selector it was created with. This is bad, misleading API design! It should have been called `$.live(selector, eventType, handler)`.
bobince
This was exactly my problem, Thanks Petersen. Cached selectors, while normally preferable because you don't have to query the dom again, give .live() trouble. Don't do this like I did, it won't work: $("dd", this._$element).live("click", function (event) {});
Simple As Could Be
A: 

traversing is the issue and you can solve it with a simple selection. you could do this

var obj = $(this),
    obj.find("legend").addClass('SmartFieldSetCollapsible');
$('legend.SmartFieldSetCollapsible').live('click.collapsible', function(e){

hope it helps. Cheers

kaimnryu