If the id
of element is not known, but its value
is known and the id
of its parent is known, the following can be done
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>
And now if one wanted to remove all the pay later elements (along with label elements), the ones with value 2
$('#payment').find("input[value=2]").each(function () {
$(this).remove();
$('label[for=' + $(this).attr('id') + ']').remove();
});
label
s are the only elements with thefor
attribute (orhtmlFor
property), you might as well specify thelabel
in the selector, and avoid the implied universal selector (just to cut down on the work that has to be done to find the element). – Bookstall