Tiny
Tiny

Reputation: 363

Remove label for dynamicaly created checkbox

I'm trying to delete both the checkbox and the label on success of json call. I have tried alsorts of code found here but so far have only managed to remove the checkbox.

Checkbox and label code

      <label class="checkbox"><input type="checkbox" id="<?php echo $row['name']; ?>" value="<?php echo $row['id']; ?>" name="delete[]" class="checkboxAdmin" /><?php echo $row['name']; ?> - <?php echo $row['email']; ?></label>

Jquery code

    $(".delete_admin_but").click(function() {

$.post("includes/delete_admin.inc.php",{ checked_box : $('input:checkbox:checked').val()},function(json)   {
    if(json.result === "success") {
        $("#deleteAdminError").html(json.message);
        $("input:checkbox:checked").remove();

//  $('.add_intern').get(0).reset();
    }else{
        $("#deleteAdminError").html(json.message);
    }
});
        });//submit click

The checkbox and label are dynamically created, and the only code I have found is for checkboxes and labels that are embeded in code with specific id that is easily identified, I hope that makes sense

Upvotes: 0

Views: 577

Answers (2)

Capsule
Capsule

Reputation: 6159

If I understand well...

Replace

$("input:checkbox:checked").remove();

With

$("input:checkbox:checked").parent().remove();

Removing the parent (in your case that's the label) will remove the label and input altogether

Upvotes: 1

kapantzak
kapantzak

Reputation: 11750

Try selecting the checkbox parent an remove it:

if(json.result === "success") {
    $("#deleteAdminError").html(json.message);
    $("input:checkbox:checked").parent('label').remove();
}

Upvotes: 0

Related Questions