Where does DOM manipulation belong in Angular 2?

Based upon recommend solution by developers: http://angularjs.blogspot.de/2016/04/5-rookie-mistakes-to-avoid-with-angular.html @Component({ selector: ‘my-comp’, template: ` <div #myContainer> </div> ` }) export class MyComp implements AfterViewInit { @ViewChild(‘myContainer’) container: ElementRef; constructor() {} ngAfterViewInit() { var container = this.container.nativeElement; console.log(container.width); // or whatever } } Attention: The view child name has to begin with myName and in the template you … Read more

How to move all HTML element children to another parent using JavaScript?

Basically, you want to loop through each direct descendent of the old-parent node, and move it to the new parent. Any children of a direct descendent will get moved with it. var newParent = document.getElementById(‘new-parent’); var oldParent = document.getElementById(‘old-parent’); function move() { while (oldParent.childNodes.length > 0) { newParent.appendChild(oldParent.childNodes[0]); } } #old-parent { background-color: red; } … Read more

How to add a class to a given element?

If you’re only targeting modern browsers: Use element.classList.add to add a class: element.classList.add(“my-class”); And element.classList.remove to remove a class: element.classList.remove(“my-class”); If you need to support Internet Explorer 9 or lower: Add a space plus the name of your new class to the className property of the element. First, put an id on the element so … Read more