Jason Jason - 1 year ago 113
Javascript Question

Remove Multiple Elements of same class in Angular but leave first one

I am trying to remove all elements of same class but leave the first one. How can I achieve this in angular?

var myElement = angular.element( document.querySelector( 'myclass') );
myElement.not(':first').remove() //removes element

This is the error I am getting:
ionic.bundle.js:21157 TypeError: myElement.not is not a function

Answer Source

The .not() function isn't defined in jqLite, angular.element, acording to this docs, doesn't implements all jQuery selectors and methods.


Here is an example.

var myElements = document.getElementsByClassName( 'myclass');

myElements = Array.prototype.slice.call( myElements ); // convert htmlcollection to array
myElements = myElements.splice(0, 1); //removes first from working list

angular.element(myElements).remove();   //removes remaining elements from the DOM
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download