Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Is there an Angular way of doing: document.getElementById(id).style.display = "none";

I have a div with the id of 1. I'm trying to set the display to none dynamically. Is there an Angular way of doing this. Currently, I'm using vanilla javascript. I was asking about doing this dynamically because there will be over 60 divs that will be created from an array.

In my html

<div *ngFor="let item of items; i = index;">
    <div id={{i}} (click)=hideDiv()></div> 
</div>

In my method

hideDiv() {
return document.getElementById('1').style.display = "none";
}

That works but I'm looking for the Angular way of doing the above.

It was suggested that I use @ViewChild. Here's what I've changed. I can't use a Template Reference Variable as the html divs are created dynamically. Unless someone can let me know how to create the template variables dynamically. Although I don't think it's possible to create template variables with a loop.

@ViewChild('imgId', { static: true }) elementRef: ElementRef<HTMLDivElement>;
imgId: string;

Then in the method I have:

this.imgId = event.path[0].attributes[1].value;
 this.elementRef.nativeElement.style.display = "none";

The event.path[0].attributes[1].value gets me the id of the image. The imgId shows when I console log it. It's still not changing the display on the div to none. Also I'm getting the error: Cannot read properties of undefined (reading 'nativeElement')

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Yes, you can use the ViewChild query in Angular to do this. In your component, define a query like this:

@ViewChild('#1') elementRef: ElementRef<HTMLDivElement>;

Implement the AfterViewInit interface in your component, and inside it, use this:

this.elementRef.nativeElement.style.display = "none";
about 4 years ago · Santiago Gelvez Report

0

You can simply use ngIf for this

Component

  shouldDisplay: boolean = true;

  hide(): void {
    this.shouldDisplay = false;
  }
  show(): void {
    this.shouldDisplay = true;
  }

Html

<button (click)="hide()">Hide</button>
<button (click)="show()">Show</button>
<div *ngIf="shouldDisplay">this is the content</div>

Here is the working example

about 4 years ago · Santiago Gelvez Report

0

This is the Angular way: template

<div *ngIf="showMe"></div>

or

<div [hidden]="!showMe"></div>

TypeScript:

showMe: boolean;

hideDiv() {
    this.showMe = false;
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!