Search code examples
javascriptjqueryhtmlgridster

Make this widget for jQuery-plugin work?


I am using the jQuery plugin Gridster.

I have made an add_widget button which adds a new widget. This widget can be deleted again also.

All this is working properly. But when you click the header it should trigger a sliding box. But this sliding box doesn't work on newly added widget, only on the widgets that were there from the start.

HELP!!!!

See this fiddle: http://jsfiddle.net/ygAV2/

I suspects the:

addbox part

//add box
$('.addbox').on("click", function() { 
gridster.add_widget('<li data-row="1" data-col="1" data-sizex="2" data-sizey="1"><div class="box"><div class="menu"><header class="box_header"><h1>HEADER 5</h1></header><div class="deleteme"><a  href="JavaScript:void(0);">delete me ;(</a></div></div></li>', 2, 1)

});

and the sliding box part:

// widget sliding box

 $("h1").on("click", function(){
   if(!$(this).parent().hasClass('header-down')){
      $(this).parent().stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down');
   } else{
      $(this).parent().stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
   }
});



$(document).click(function() {
    if($(".box_header").hasClass('header-down')){
        $(".box_header").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
}
});

$(".box_header").click(function(e) {
    e.stopPropagation(); // This is the preferred method.
           // This should not be used unless you do not want
                         // any click events registering inside the div
});

Solution

  • The use of .live() is deprecated. http://api.jquery.com/live/

    So using the correct way, .on(event, selector, handler) on all your click events.

    You will achieve your desired result.

    Here is a code snippet

    $(document).on("click", 'h1', function() {
        if (!$(this).parent().hasClass('header-down')) {
            $(this).parent().stop().animate({
                height: '100px'
            }, {
                queue: false,
                duration: 600,
                easing: 'linear'
            }).addClass('header-down');
        } else {
            $(this).parent().stop().animate({
                height: '30px'
            }, {
                queue: false,
                duration: 600,
                easing: 'linear'
            }).removeClass('header-down');
        }
    });
    

    and here

    //remove box
    $(document).on('click', ".deleteme", function () {
        $(this).parents().eq(2).addClass("activ");
        gridster.remove_widget($('.activ'));
        $(this).parents().eq(2).removeClass("activ");
    });
    

    and also here

    $(document).on("click", ".box_header", function (e) {
        e.stopPropagation(); // This is the preferred method.
        // This should not be used unless you do not want
        // any click events registering inside the div
    });
    

    I have updated your jsfiddle: http://jsfiddle.net/ygAV2/2/