Angular 6 + get elements with class name from document and change style

12,516

Calling showTab() in ngOnInit() means it's called before the children have been initialized.

You need to use ngAfterViewInit() hook.

Full list of component lifecycle hooks.

Share:
12,516
Renjith
Author by

Renjith

Ever since my father bought me a computer in 2001 (Win 98, Intel P3 :)), I am addicted to programming. Staring at the blue screen Turbo C++ till the world of java, Eclipse... I really enjoy doing it. It was in 2007 when I got introduced to Java for the first time. I chose it over .NET because of its popularity. Even though I was good at completing any given task, it was Kathy Sierra's (Sun Certified Java Programmer 1.4) book which opened the magical world of Object Oriented Programming for me. It was my reference book for Sun certification. I am still driven by the level of confidence I got after reading that amazing book.

Updated on June 05, 2022

Comments

  • Renjith
    Renjith almost 2 years

    I have a set of "tabs" in my HTML page. They are nothing but "div" with a class name "tab". My intention is to show one tab at a time and use "Next" and "Previous" buttons to access other tabs. HTML code looks like this.

    <div class="container">
      <div *ngFor="let question of questionList;let i = index" class="questionBox tab">
    
    
        {{question.question.query}}
    
        <br>
        <div *ngFor="let option of question.options">
          <input type="radio" value="{{option.id}}" name="radio_{{i}}" [(ngModel)]="question.selected.id">{{option.text}}
          <br>
        </div>
        {{question.selected | json}}
    
        <br>
        <br>
        <!--TO DO -->
        <input type="button" class="btn btn-primary" value="Previous">
        <!--TO DO -->
        <input type="button" class="btn btn-primary nxtButton" value="Next">
      </div>
      <input type="button" class="btn btn-success btn-block" value="Submit">
    </div>
    

    Initially all the tabs are hidden with CSS.

    .tab{
        display: none; 
    }
    

    on page initialization, first tab has to be displayed and rest are hidden. showTab() method in the component class is intended for that purpose. shotTab method accepts a "number" that represents the index of the tab to be displayed.

      public showTab(n: number){
    
        let tabElements = document.getElementsByClassName("tab");
        let tabToDisplay = tabElements.item(n) as HTMLElement;
        tabToDisplay.style.display="block";
      }
    

    to show the first tab, showTab() methos is invoked from ngOnInit() method of the component. but the line let tabElements = document.getElementsByClassName("tab"); does not return any result. i.e. the length of tabElements is 0. So the application will fail saying that "Cannot read property 'style' of null".

    AppComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'style' of null
        at AppComponent.push../src/app/app.component.ts.AppComponent.showTab (app.component.ts:25)
        at AppComponent.push../src/app/app.component.ts.AppComponent.ngOnInit (app.component.ts:18)
        at checkAndUpdateDirectiveInline (core.js:9243)
        at checkAndUpdateNodeInline (core.js:10507)
        at checkAndUpdateNode (core.js:10469)
        at debugCheckAndUpdateNode (core.js:11102)
        at debugCheckDirectivesFn (core.js:11062)
        at Object.eval [as updateDirectives] (AppComponent_Host.ngfactory.js? [sm]:1)
        at Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)
        at checkAndUpdateView (core.js:10451)