Adjust the color of an SVG upon clicking a button using TypeScript

I am looking to dynamically change the fill color of my SVG when a button is clicked. While I have been successful in changing it through external CSS using the npm package angular-svg-icon, I am unsure how to achieve this using TypeScript. I came across a similar query on StackOverflow but unfortunately, no solution was provided to the user: svg change rectangle Color on button ng-click

circle.svg:

<svg id="bars" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 63.15 224.35">
    <circle class="cls-1" cx="50" cy="50" r="50" fill="#529fca" />
</svg>

tab1.page.html:

<ion-content>
    <svg-icon [applyCss]="true" src="assets/images/circle.svg" [svgStyle]="{ 'height.px':200, 'width.px':200 }" />
    <ion-button (click)="ChangeColor()">Click</ion-button>
</ion-content>

tab1.page.scss:

#bars{
    position: absolute;
    top: 80px;
    right: 80px;
    width: 200px;
    height: 100px;
}

.cls-1 {
    fill:blue;
}

Here is the function where I aim to modify the SVG color: ChangeColor(){ }

Answer №1

To implement conditional styling on your SVG element, you can use Angular's NgClass directive and apply CSS to customize the fill color. Here's an example:

<rect [ngClass]="{'highlight': showHighlight}"></rect>

In your CSS file:

.highlight {
      fill: red;
}

Then, in your component's method, toggle the showHighlight variable to true to change the color dynamically.

Check out this StackBlitz demo for a live example!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to superimpose a post-type title onto the featured image?

I have crafted this WP_Query to extract the titles and featured images from my post types. <div class="job-cont"> <?php $query = new WP_Query( array( 'post_type' => 'jobs' ) ); if ( $query->have_posts( ...

How to resolve logic issues encountered during Jasmine testing with describe()?

I am encountering an issue while testing the following code snippet: https://i.sstatic.net/ImwLs.png dateUtility.tests.ts: import { checkDayTime } from "./dateUtility"; describe("utilities/dateUtility", () => { describe("ch ...

Attempting to place a different span beneath the current span

Is it possible to add another span below the first span and keep it on the same line as the circle? Check out this link for reference The following is the HTML code snippet: <div class="dialog-box"> <div class="dialog-box-circle"></d ...

Is there a way to have only the <a> tag be clickable in an unordered list, without making the entire list item clickable?

I just need the text to be made clickable instead of the entire list item block. <a class="menu_head">Hello</a> <ul class="menu_body"> <li><a href="#">Sub-menu 1</a></li> <li><a href="#">Sub-menu 2 ...

position:fixed - disparities between Firefox and Chrome browsers

I'm currently utilizing the Skeleton boilerplate to craft a responsive design. Here is how it appears in Chrome and Firefox: Chrome: Firefox: Visually, the gray bar intended to be the menu displays correctly in Chrome. My objective is to create a ...

Tips for displaying a hyperlink in an Angular 6 component template

In my Angular 6 application, I am facing an issue with a component that is used to display messages on the page. Some of the messages include hyperlinks in HTML markup, but they are not being rendered properly when displayed (they appear as plain text with ...

Error encountered with Typescript implementation of Passport JS Google OAuth 2.0 Strategy

Currently in the process of migrating a passport JS Google OAuth2 strategy from JavaScript to TypeScript, encountering a TypeScript compile error. The clientID and ClientSecret fields are showing no overload matching the call. Despite both options being de ...

Having difficulty transferring navigation props between screens using react-navigation

Within my ContactList component, I have utilized a map to render various items. Each item includes a thumbnail and the desired functionality is that upon clicking on the thumbnail, the user should be directed to a new screen referred to as UserDetailsScree ...

I am currently working on configuring a webhook utilizing the Stripe platform with NextJS version 13.2.3

Upon successfully checking out my cart using the built-in Stripe page, I am redirected to the successUrl route. During this process, my local test webhook is triggered as expected. However, I encountered some errors when attempting to verify that the reque ...

Capture a snapshot of a webpage that includes an embedded iframe

Currently, we have a nodeJS/angular 4 website that contains an iframe from a third party (powerBI Emebdded). Our goal is to develop a feature that allows the end user to capture a screenshot of the entire page, including the content within the iframe. We ...

Tips for customizing styles when an element is being dragged over in Material-UI?

If I want to alter the backgroundColor when hovering, I can utilize sx={{"&:hover":{backgroundColor:"yellow"}} Is there a way to adjust the backgroundColor on dragover? It appears that sx={{"&:dragOver":{backgroundCol ...

Tips for positioning the calendar icon inside the input field using Angular Material

Just beginning my journey with Angular Material and currently focusing on building a datepicker. I am looking to reposition the icon from outside of the input field to inside. After trying out various Material themes, none seem to have this specific style. ...

Creating a gradient border with CSS on the bottom edge

I need help with a design challenge I am facing. I am trying to add a simple 40px height vertical border at the bottom of a Bootstrap 5 navbar, transitioning from color #e5e5e5 to color #fefefe. I have included a sample image for reference: https://i.ssta ...

Develop a "Read More" button using Angular and JavaScript

I am in search of all tags with the class containtText. I want to retrieve those tags which have a value consisting of more than 300 characters and then use continue for the value. However, when I implement this code: <div class=" col-md-12 col-xl-12 c ...

Question About jQuery and CSS3

Can someone suggest an effective method for modifying CSS3 with jQuery? I was thinking of using a selector like this: $("#con_back").css('-webkit-border-top-right-radius','0px'); $("#con_back").css('border-top-right-radius', ...

Using NodeJS API gateway to transfer image files to S3 storage

I have been attempting to upload an image file to S3 through API Gateway. The process involves a POST method where the body accepts the image file using form-data. I crafted the lambda function in TypeScript utilizing the lambda-multipart-parser. While it ...

occupying half of the screen

Having trouble displaying two videos in an ionic grid. My goal is to have both videos take up the entire screen, with each occupying 50% height and 100% width. However, when I try to achieve this, the ion-row only takes up 50% of the screen and the video i ...

There is a potential for the object to be 'undefined' when calling the getItem method on the window's local storage

if (window?.sessionStorage?.getItem('accessToken')?.length > 0) { this.navigateToApplication(); } Encountering the following error: Object is possibly 'undefined'.ts(2532) Any suggestions on how to resolve this issue? I am attem ...

Checking for file existence using the HEAD command can be done by specifying the file path as

I am seeking to utilize the Spring Endpoint provided below for file uploading. @PostMapping(value = "/upload", produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<StringResponseDTO> uploadFile(@RequestParam("file") MultipartFi ...

Inconsistent behavior with CSS Grid column widths in Firefox versus Chrome

As a beginner in CSS Grid, I recently encountered an issue where the layout differed between Firefox and Chrome. It seems like Firefox is adhering strictly to the grid width specifications set with "grid-template-columns", whereas Chrome is adjusting based ...