Search code examples
javascripthtmljqueryinputremovechild

Jquery remove all inputs with same class except one


I want to remove all input labels with same class except one in div #tikersDiv. I got this script but it doesn't work:

  $('#tikersDiv').children('label').each(function(i){
  let removed = $('#id_' + (++i)).children('label').each(function(){
     $(this).remove();
  });
  $(this).append($(removed[0]).html());
  $(this).append($(removed[3]).html());
});

Solution

  • Ok, I've got this. If anybody want to remove all child elements with same class except one in div, just use this script. I hope I will help somebody

    var classes = $('#tikersDiv label').map(function(){
        return $(this).attr('class');
        }).get();
    
    $.each(classes, function(value, index){
         $('#tikersDiv').find('.' + index + ':not(:eq(0))').remove();
         });