user1064285
user1064285

Reputation: 165

JQuery remove label checkbox

I have the following code to create checkboxes (puntosD is a div I created where I append the checkbox):

$("div.puntosD").append('<input type=\"checkbox\" id="checkpuntos" name="'+data[index].id+'" value="'+data[index].nombre+'"> '+data[index].nombre);

I have the following code to remove the checkboxes:

$("input[type='checkbox']").next('label').remove();
$("input[type='checkbox']").remove();

This however only removes the actual boxes to tick but not the labels, how can I do this?

Thank you!

Upvotes: 2

Views: 6241

Answers (2)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

How about this, wrap the checkbox and label in span:

$("div.puntosD").append('<span><input type=\"checkbox\" id="checkpuntos" name="'
     + data[index].id+'" value="'+data[index].nombre+'">'
     + data[index].nombre+'</span>');

remove the span:

$("input[type='checkbox']").parent('span').remove();

Upvotes: 3

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

I am not sure if you are trying to remove all the checkbox & label inside the div or remove specific checkbox & label inside the div.

You can use .empty on that div in case if you only had checkbox & label inside the div and you want to remove them all.

$('div.puntosD').empty();

If you want to remove specific, then wrap it with a span and remove the span using below code,

$(':checkbox[name="1"]').parent('span').remove();

DEMO here

Upvotes: 1

Related Questions