I have a function which deletes row of a table.
Code goes like this:
jQuery(function() {
initDeleteRow();
});
function initDeleteRow() {
var containers = $('.management-section');
$.each(containers, function() {
if ( containers.length ) {
var container = $(this);
var deleteBtn = container.find('.activity-selected.delete');
var individualDeleteBtn = container.find('.delete-row');
deleteBtn.on('click', function(e) {
var checkedBox = $(this).closest('.management-section').find('tbody .selection-check:checked');
if ( checkedBox.length ) {
setTimeout(function() {
checkedBox.closest('tr').remove();
setTimeout(function() {
container.find('.activity-buttons').removeClass('selected-buttons').addClass('second-animation');
setTimeout(function() {
container.find('.activity-buttons').removeClass('second-animation');
}, 1040);
}, 100);
}, 1200);
}
if($(this).closest('.management-section').find('tbody .selection-check:checked').length === $(this).closest('.management-section').find('tbody .selection-check').length) {
$(this).closest('.management-section').find('.check-all').prop("checked",false);
}
});
}
});
}
However, I need to use some part of it in multiple occasions. For example,
deleteBtn.on('click', function(e) {
// code inside this section needs to be used in another place
});
individualDeleteBtn.on('click', function(e) {
// same code as above should run here with some small change such as it's not checkbox.
});
i tried putting everything inside a function like this:
deleteBtn.on('click', function(e) {
deleteRowFunc();
});
function deleteRowFunc() {
// all codes inside of deleteBtn.on('click', function(e) moved here
}
but it didn't work at all. not even any error in console. What am i missing here?
This is how the code looks after my change:
working Fiddle: https://jsfiddle.net/4rwvaey9/2/ not working Fiddle: https://jsfiddle.net/4rwvaey9/1/
setTimeout()so it's probably the former). If you are looking for an agnostic answer cut back on the code dumping. If you're looking to resolve the issue so it works, include the HTML but try to isolate to the most relevant code. Read this section on minimal reproducible example