jQuery: Finding duplicate ID's and removing all but the first

走远了吗. 提交于 2019-11-28 19:40:57

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();
});
Mahesh Reddy

Try:

 $('[id="' + this.id + '"]:not(#" + this.id + ":first)').remove();

you can try

$("#ID").nextAll().remove();
$('[id]').each(function() {
   var $ids = $('[id=' + this.id + ']');
   if ($ids.length > 1) {
     if(this.id === your_id)//which is duplicating
         $ids.not(':first').remove();
     }
});

This code is longer than some of the others, but the double-nested loop should make its operation obvious.

The advantage of this approach is that it only has to scan the DOM to generate the list of elements with an id attribute once, and then uses the same list to find (and remove) the duplicates.

Elements that were already removed will have parentNode === null so can be skipped while iterating over the array.

var $elems = $('[id]');
var n = $elems.length;

for (var i = 0; i < n; ++i) {
    var el = $elems[i];
    if (el.parentNode) {  // ignore elements that aren't in the DOM any more
        var id = el.id;
        for (var j = i + 1; j < n; ++j) {
            var cmp = $elems[j];
            if (cmp.parentNode && (cmp.id === id)) {
                $(cmp).remove();  // use jQuery to ensure data/events are unbound
            }
        }
    }
}

try this

var duplicated = {};

$('[id]').each(function () {   

    var ids = $('[id="' + this.id + '"]');

    if ( ids.length <= 1 ) return  ;

    if ( !duplicated[ this.id ] ){
         duplicated[ this.id ] = [];   
    }       

    duplicated[ this.id ].push( this );

});

// remove duplicate last ID, for elems > 1 
for ( var i in duplicated){

    if ( duplicated.hasOwnProperty(i) ){  

             $( duplicated[i].pop() ).remove();            
    }
}

and jsfiddle is http://jsfiddle.net/z4VYw/5/

I'd use not() to remove the current element from ids and remove the rest:

(Fiddle)

$('body').on("click", ".placeholder", function() {

    data = '<section id="e1"><input name="e1name" /></section><section id="two"><input name="two" value="two section" /></section>';

    $('form').append(data);

        // ideally I'd like to run this function after append but after googling I find that's not possible.
        // for each ID ...
        $('[id]').each(function () {

            var ids = $('[id="' + this.id + '"]');

            if (ids.length>1) {                    
                ids.not(this).remove();            
            }

            return;

        });

});

Try Using The below function Works fine for me:

 $('#favourities-ajax ul li').each(function(i) {
      $('[id="' + this.id + '"]').slice(1).remove();
      });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!