Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, December 13, 2015

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

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


    $('[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.

Answer by Mahesh Reddy for jQuery: Finding duplicate ID's and removing all but the first


Try:

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

Answer by Noval Agung Prayogo for jQuery: Finding duplicate ID's and removing all but the first


to exclude the first element, you could use jquery :gt(0) selector:

$('[id]').each(function () {      $('[id="' + this.id + '"]:gt(0)').remove();  });  

or by using javascript slice(1) method:

$('[id]').each(function (i) {      $('[id="' + this.id + '"]').slice(1).remove();  });  

Answer by Jon Egerton for jQuery: Finding duplicate ID's and removing all but the first


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

(Fiddle)

$('body').on("click", ".placeholder", function() {        data = '
'; $('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; }); });

Answer by rab for jQuery: Finding duplicate ID's and removing all but the first


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/

Answer by user2235386 for jQuery: Finding duplicate ID's and removing all but the first


you can try

$("#ID").nextAll().remove();  

Answer by Alnitak for jQuery: Finding duplicate ID's and removing all but the first


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              }          }      }  }  

Answer by user2254898 for jQuery: Finding duplicate ID's and removing all but the first


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.