How to remove a single widget from gridster.js by his dynamically created id

iamrobertsillo picture iamrobertsillo · Dec 20, 2013 · Viewed 9.4k times · Source

I need to know how can I remove a single gridster.js widget by his dynamically created id with gridster.add_widget. Every new widget created has an own button to remove that single widget, but I can't make it work.

Here is my Code

$(document).ready(function(){

var count = 0;
var newid = count + 1;

    $(document).on("click", "#newGrid", function() {
        var gridster = $(".gridster ul").gridster().data('gridster');
        gridster.add_widget('<li id="block"'+newid'>Hello, now delete me <span id="remove"'+newid'>x</span></li>',2 ,1);
    });

    $(document).on('click', '#remove'+newid, function() {
    var gridster = $(".gridster ul").gridster().data('gridster');
        gridster.remove_widget( '#block'+newid );
    });

});

For adding widgets it works fine, but I can't remove widgets.

Answer

minimal picture minimal · Jan 20, 2014

Actually you don't need ID for widget removing. Please check my script.

    var gridster = $('.gridster ul').gridster().data('gridster');
    $(document).on( "click", ".gridster ul li", function() {
        $(this).addClass("activ");
        gridster.remove_widget($('.activ'));
    });