var deleteRow = $('<%= params[:element] %>'); var refreshUrl = '<%= params[:refresh_url] %>'; var refreshFunc = function(url) { $.ajax({ url: url.length > 0 ? url : window.location.href, method: 'GET', dataType: "script" }) } if(deleteRow.length > 0){ var needRefresh = deleteRow.siblings().length == 0; deleteRow.remove(); if(needRefresh){ refreshFunc(refreshUrl); } } else { refreshFunc(refreshUrl); }