jQuery remove checkbox and associated label

Homer_J picture Homer_J · Dec 13, 2013 · Viewed 18.7k times · Source

I have the following:

<input type="checkbox" class="oDiv" id="Parent"  name="divcorp[]" value="Parent"/>
<label for="Parent">Parent</label>

I can remove the checkbox using the following, which works correctly:

$( "#Parent" ).remove();

However, how could I also remove the associated label for this checkbox?

Answer

Adil picture Adil · Dec 13, 2013

You can use attribute equal selector

Live Demo

$('label[for=Parent]').remove();

Description: Selects elements that have the specified attribute with a value exactly equal to a certain value.