zer0
zer0

Reputation: 5017

Change img class attribute on error

My HTML:

<img *ngIf="someCondition" src="assets/images/type-{{object.plastic}}.png" id="type_img" class="select-img" alt="Plastic" onError="console.log(this.class)"/>

I am trying to change the class of img attribute if the image is not found. When I console out this.class, I get undefined but if i try with this.src I get the src value. How can I change the class?

Upvotes: 1

Views: 935

Answers (1)

Artem Arkhipov
Artem Arkhipov

Reputation: 7455

Use this.className instead. There is not 'class' property on DOM Element in JS.

Also remember that className property contains all the classes of element separated by space.

For example, HTML:

<button id="my-btn" class="btn btn-big btn-red"></button>

JS:

let myBtn = document.getElementById('my-btn');
console.log(myBtn.className); // "btn btn-big btn-red"

Also, please note, that if you are using Angular or AngularJS you should better take a look on ngClass directive and use it when possible.

Upvotes: 1

Related Questions