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.
Since it works with jQuery objects, you don't need to add any classes or ids to identify the item you want to remove. Just create a click event and reference to $(this).
Or something more useful, where you have a "remove button" inside the li:
please check code below, should work with you
Actually you don't need ID for widget removing. Please check my script.
In fact, you just need to wrap your
'#block'+newid
in a jQuery element, as stated in the function comment:Function comment (in jquery.gridster.js):
* @param {HTMLElement} el The jQuery wrapped HTMLElement you want to remove.
this works for me, i use a timestamp as unique id
ADD AN ITEM:
REMOVE AN ITEM: