4

I am trying to set property to html element but getting an error Type 'HTMLCollectionOf' is not assignable to type 'HTMLElement'

Here is my typescript code---

let elem: HTMLElement= document.getElementsByClassName('notification-top-bar');
      setTimeout(elem.setAttribute("style", "display:none;"), this.duration);

Here is my HTML code---

<div class="row">
  <div *ngFor="let n of notifications" class="notification-top-bar" [ngClass]="type" style="display: block;">
    {{message}}  
    <a class="close glyphicon glyphicon-close" (click)="onCloseNotifications($event)"></a>
    <p>{{n}}</p>
  </div>
</div>

1
  • 1
    The method's name is already telling you: "getElementsByClassName". It returns multiple elements and you try to assign it to a single HTMLElement Commented Jun 7, 2018 at 9:01

3 Answers 3

6

Use ViewChildren to get the elements and set their styles. For that you'll have to add a template reference for the elements using which you can get them in your code. Change your template code to add a reference e.g. #notification:

<div class="row">
    <div *ngFor="let n of notifications" #notification 
         class="notification-top-bar" [ngClass]="type" 
         style="display: block;">
        {{message}}
        <a class="close glyphicon glyphicon-close" 
           (click)="onCloseNotifications($event)"></a>
        <p>{{n}}</p>
    </div>
</div>

Then you can get that in your code like this using QueryList:

import { Component, ViewChildren, QueryList } from '@angular/core';

@Component({ ... })
export class YourComponent  {

  // Get the template elements
  @ViewChildren('notification') private _notificationsElements:  QueryList<ElementRef>;

  ngAfterViewInit() {    

      this._notificationsElements.forEach((element)=>{      
      const htmlElement = element.nativeElement as HTMLElement;
      // Assigning the property directly
      setTimeout(htmlElement.style.display = 'none',this.duration);
      // OR
      // Using the setAttribute method
      setTimeout(htmlElement.setAttribute("style", "display:none;"),this.duration);
    });

  }
}

StackBlitz Demo.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this way but Its throwing an error " Property 'nativeElement' does not exist on type 'HTMLElement' ". Is there something which I am missing..?
You can use ElementRef to avoid that error. Check the updated demo and answer.
3

document.getElementsByClassName returns an array and not single Element. you can get element by specifying its index like

document.getElementsByClassName('notification-top-bar')[index]

also i think you have a typo here , it should be display

setTimeout(elem.setAttribute("style", "display:none;"), this.duration);

2 Comments

I tried giving index but it shows an error "Type 'Element' is not assignable to type 'HTMLElement' " let elem: HTMLElement= document.getElementsByClassName('notification-top-bar')[0]; setTimeout(elem.setAttribute("style", "display:none;"), this.duration);
working for me , hiding the html successfully. check here
0

I think you`d better assing and id

<div id="notification_id" *ngFor="let n of notifications" class="notification-top-bar"

and get it this way:

document.getElementById("notification_id")

If you do it with classname perhaps you dont know the position of the element.

1 Comment

I have tried using id, still it shows an error "Cannot read property 'setAttribute' of null"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.