title: How to access dynamically created buttons in angular?
body: <p>So, I am creating a quiz application where I have a scrollbar for questions and in that scrollbar I have buttons depending on the length of a question set. So I've created those buttons using *ngFor directive. Now what I want to do is whenever a user selects any option (mcq), the question buttons in the scrollbar should get highlighted in following way:</p>
  <ol>
  <li>If user selects any option, then change the question button color to Green</li>
  <li>If user skips the question, then change the question button color to Yellow</li>
  </ol>
  <p>HTML Code for Question Scrollbar:</p>
  <pre><code>&lt;div id=&quot;answer-buttons&quot; class=&quot;ques-grid &quot;&gt;
              &lt;button #navBarButton *ngFor=&quot;let item of items&quot;&gt;{{item}}&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I'm have tried doing it by first accessing the buttons using ViewChild in ts file and then apply some logic, but it's not working, it is only changing the color of first button</p>
  <pre><code>@ViewChild('navBarButton',{ static: false }) navBarButton:ElementRef
  //and in some function I've tried this logic
  if(this.attemptedQuestionCount[this.currentQuestionIndex]==1){
    this.navBarButton.nativeElement.style.backgroundColor = &quot;#228B22&quot;
  }
  else{
    this.navBarButton.nativeElement.style.backgroundColor = &quot;#FCF55F&quot;
  }
  </code></pre>
  <p>How can I achieve my objective?</p>