Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
select single image on click <img> tag

I have rendered multiple images using tag. I want to change the background color of image when it is clicked. But when I click on one image, the color of all the images changed.

Here is my code. I am working in Angular.

html

<img
 [src]="data.Url" alt="image"
 (click)="_upload(data.Url)" 
 [ngClass]="{'green' : toggle, 'red': !toggle}"
>

ts

inside my function

this.toggle = !this.toggle;
this.status = this.toggle ? 'Enable' : 'Disable';
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

For rendering multiple html elements, I assume you are using the ngFor directive, so you could use the index for giving each element a different css class.

For example:

HTML

<div *ngFor="let option of options; let i = index">
 <img
   [src]="option.Url"
   (click)="_upload(option.Url, i)"
   [class.selected]="i === indexSelected" <!--apply the selected css class if condition is TRUE-->
  >
</div>

css

.selected {
  background-color: green;
}

ts

...
export class YourComponent {
  indexSelected: number = -1;

  _upload(url: string, index: number) {
    // ...
    this.indexSelected = index;
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Thanks all I did this in this way

<td *ngFor="let data of responseData; let i= index;" (click)="active= i" [ngStyle]="{'background-color': active === i ? '#4b8df2' : 'white' }">
    <div>
    <img 
     [src]="data.Url" alt="image"
     (click)="_upload(data.Url)" 
    >
    </div>
</td>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda