sradha sradha - 2 months ago 6
jQuery Question

How to remove multiple tr simultaneously in jQuery without using for loop

I can do this, simply doing it in for loop, but I need to do it without using loop.

Is there any way to make it possible?
Below I am writing some of my code:



function submitFormArchive(Status) {
var siteUrl = $("#pageurl").val();
$("#archive-loader").show();
var myCheckboxes = new Array();
$("input[type='checkbox'][name='archive[]']:checked").each(function () {
myCheckboxes.push($(this).val());});
alert(myCheckboxes);///this one i am getting dynamic ids
$.ajax({
type: 'POST',
url: siteUrl + 'Appadmins/ajaxUpdateArchive',
data: {"myCheckboxes": myCheckboxes, "status": Status},
success: function (data) {

$("#remove-id-"+myCheckboxes).slice(myCheckboxes).remove();
}
});
return false;
}


<tr class="message_box" id='remove-id-<?php echo $list->id;?>'></tr>

This one in for loop

I am posting some part of codes.




$("#remove-id-" + myCheckboxes).slice(myCheckboxes).remove();


Is there any for multiple, maybe I have written wrong in this above code.

Answer

Try the code below

$("input[type='checkbox'][name='archive[]']:checked").each(function() {

    myCheckboxes.push($(this).val());
});

alert(myCheckboxes); //40,39,38 //it can be any id ,as i made it dynamic .

// construct array of IDs here
var ids = myCheckboxes.map(function(id) { return "#remove-id-" + id; });
// ["#remove-id-40", "#remove-id-39", "#remove-id-38"]

// concat all id selectors together using comma `,` 
var idSelector = ids.join(",");
// "#remove-id-40,#remove-id-39,#remove-id-38"

// remove all at once.
$(idSelector).remove();