$('[id]').each(function () {
var ids = $('[id="' + this.id + '"]');
// remove duplicate IDs
if (ids.length > 1 && ids[0] == this) $('#' + this.id).remove();
});
The above will remove the first duplicate ID, however I want to remove the last. I've tried $('#'+ this.id + ':last')
but to no avail.
Fiddle
In the fiddle the input with the value 'sample' should be kept when the append action takes place.
Use jquery filter :gt(0)
to exclude first element.
$('[id]').each(function () {
$('[id="' + this.id + '"]:gt(0)').remove();
});
Or select all the available elements, then exclude the first element using .slice(1)
.
$('[id]').each(function (i) {
$('[id="' + this.id + '"]').slice(1).remove();
});
Try:
$('[id="' + this.id + '"]:not(#" + this.id + ":first)').remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With