2

How can I change the image of a button when I click it?

I have a button and at an early stage I want it with an icon, but when I click it (active) and icon is replaced by another.

How can I do this?

Stackblitz

<!-- image1 -->
<a><img src='https://svgshare.com/i/Gwo.svg' title='' /></a>

<p></p>

<!-- image2 -->
<a style="display:none"><img src='https://svgshare.com/i/Gw7.svg' title='' /></a>

3 Answers 3

3

You can do something like that:

TS file

export class AppComponent  {
  name = 'Angular';
  value = 'https://svgshare.com/i/Gwo.svg'; //default_value

  updateImage() {
    this.value = 'https://svgshare.com/i/Gw7.svg';
  }
}

HTML file

<a><img [src]="value" title='' (click)='updateImage()' /></a>

You can also update the image each time you click on the button not just the first time using the attribute status like this:

export class AppComponent  {
  name = 'Angular';
  value = 'https://svgshare.com/i/Gwo.svg'; //default_value
  status = false;


  updateImage() {
    this.status = !this.status;
    if (this.status) //active status
     this.value = 'https://svgshare.com/i/Gw7.svg';
    else this.value = 'https://svgshare.com/i/Gwo.svg';
  }

}

Hope it helps!

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

1 Comment

I updated the solution to handle also the multi-click case on the image.
1

You may do so using just one line:

<a><img 
    [src]="toggle ? 'https://svgshare.com/i/Gwo.svg' : 'https://svgshare.com/i/Gw7.svg'" 
    title='' 
    (click)='toggle = !toggle'/>
</a>

Comments

0

You can update svg attributes on click as well as swapping the image.

For example if you had

 <svg id="svg1" xmlns="http://www.w3.org/2000/svg" style="width: 3.5in; height: 1in">
   <circle id="circle1" onclick='changerect(evt)' r="30" cx="34" cy="34" style="fill: red; stroke: blue; stroke-width: 2" />
 </svg>

You could use js to update the fill, stroke width, opacity etc.. using a function.

<svg id="svg1" xmlns="http://www.w3.org/2000/svg" style="width: 3.5in; height: 1in">
   <circle id="circle1" onclick='changesvg(evt)' r="30" cx="34" cy="34" style="fill: red; stroke: blue; stroke-width: 2" />
 </svg>

<script>
  function changesvg(evt) {
    var svgobj = evt.target;
    svgobj.style.stroke = 'yellow';
    svgobj.style.opacity = 0.5;
  }
</script>

If you wanted to swap images on click you could just use a regular swap image as ic_paty has outlined above.

4 Comments

you'd probably only use the js if the images were similar, but still.. good to know..imho
Question is regarding angular, so the answer should also be in angular format.
@Plochie I'm sure OP can figure out ng-click on his/her own.. StackOverflow is only meant to be a guide, not a code-writing service after all!
IMO, the whole point of answer is to clear the doubts. BTW in angular its (click), and in angularjs it was ng-click. Also angular has classes which does not have function prefix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.