JQuery remove checkbox and associated label

I have the following:

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

I can delete checkboxusing the following which works correctly:

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

However, how can I remove the associated shortcut for this flag?

+4
source share
2 answers

You can use the equal selector attribute

Live demo

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

Description : Selects items that have the specified attribute using a value equal to a specific value.

+9
source

If an idelement of an element is unknown, but its valueknown and idits parent is known, you can do the following:

Code ( Demo ):

<div id="payment">
    <input id="RANDOM_GENERATED-1" type="checkbox" name="div[]" value="0" />
    <label for="RANDOM_GENERATED-1">Pay Now by CC</label><br/>

    <input id="RANDOM_GENERATED-2" type="checkbox" name="div[]" value="1" />
    <label for="RANDOM_GENERATED-2">Pay Now by PayPal</label><br/>

    <input id="RANDOM_GENERATED-3" type="checkbox" name="divo[]" value="2" />
    <label for="RANDOM_GENERATED-3">Pay Later by Check</label><br/>

    <input id="RANDOM_GENERATED-4" type="checkbox" name="divo[]" value="2"/>
    <label for="RANDOM_GENERATED-4">Pay Later by Cash</label><br/>
</div>

, ( ), , 2

$('#payment').find("input[value=2]").each(function () {
    $(this).remove();
    $('label[for=' + $(this).attr('id') + ']').remove();
});
0

All Articles