7

I need to add a click event to image in angular component. I tried:

<img src="../../assets/add.svg" width="18" height="18" (click)="addItem()"> 

but this does not work. If put image inside button like this:

<button type="button" class="btn_img" (click)="addItem()">
  <img src="../../assets/add.svg" width="18" height="18"> 
 </button>

It looks like a button with an image inside, I do not want this, I want it to look like:

<img src="../../assets/add.svg" width="18" height="18"> 

but that works like:

<button type="button" class="btn_img" (click)="addItem()">
  <img src="../../assets/add.svg" width="18" height="18"> 
 </button>

How I can get this, how do you do this in angular?

5
  • Have you tried wrapping the image in a div rather than a button to see if that works? And, adding the (click)="addItem()" to the div. Something like that. Bit of a guess. Commented Jul 28, 2018 at 0:20
  • I mean, you could use an <input type="image">, but I don't know how/if that would affect Angular. Commented Jul 28, 2018 at 0:23
  • 1
    (click) functionality works on images. There is likely something else causing the issue that isn't covered in your question or an error in your DOM that you haven't caught when adding that button. Working proof of (click) working on img: stackblitz.com/edit/github-hibfhb
    – Z. Bagley
    Commented Jul 28, 2018 at 0:30
  • It works for me when putting it like OP did and putting the <img> inside a <div> or <a>. Maybe the method was written wrong? That happens to me a lot. Commented Jul 28, 2018 at 3:12
  • Any Update if solved the problem?
    – CREM
    Commented Jul 31, 2018 at 19:33

5 Answers 5

8

You can see a working example here with all the possible ways:

https://stackblitz.com/edit/angular-kvsjre?file=src%2Fapp%2Fapp.component.html

If you like Use a < div > wrapper or an anchor < a > or an image

Good luck

4

you can the click im image this way

in your view create img and input

<img [src]="image" alt="" class="image-exercise" (click)="selectFile.click()">
<input type="file" (change)="getImage($event)" style="display: none" #selectFile>

and your component create the function to save your image

getImage(ev) {
   console.log(ev.target.files[0]);
}
2
<img class="image" src="../../assets/add.svg" width="18" height="18" (click)="addItem()">

and add following class to your css file

.image {
   cursor: pointer;
}
1

some.component.html

<div >
    <img [src]="ImagePath" (click)="ImageClick()"/>
</div>

some.component.ts

export class SomeComponent implements OnInit {
    ...
    ...
    ImagePath = 'path goes here';

    ImageClick() {
        this.ImagePath = 'updated path goes here';
    }
}
0

I had a similar issue. It would work in Firefox and Internet Explorer but not in Chrome. For chrome, if all elements inside the button should respond to the click add this:

button > * {
    pointer-events: none;
}

Otherwise add to select elements as needed.

original answer from css-tricks

1
  • Hmmm... Maybe it would be better to write that as a comment (with a link to CSS-tricks). But as a new user, I know that you don't have enough points for that yet. Hmm, hmm, hmm...
    – Zeth
    Commented Apr 10, 2019 at 21:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.