Tips for changing SVG element style using JavaScript

After creating an inline SVG with multiple rectangle elements and applying the same color to all of them using CSS, I assigned unique IDs to each rectangle. However, when attempting to manipulate the rectangles' colors with plain JavaScript, the changes are not being reflected visually. The code validates correctly and the console confirms that the rectangles are selected, but the color remains unchanged. Can anyone offer suggestions on what might be wrong and how to rectify it? Thank you in advance!

Here is an example of my HTML code:

<rect id="rect1" width="40" height="230" x="20" y="170" rx="10"/>
<rect id="rect2" width="40" height="300" x="60" y="100" rx="10"/>

Below is my JavaScript code:

var rect1 = document.getElementById("rect1");
console.log(rect1);
rect1.setAttribute("style", "color: red");

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

Greetings all! I am curious about the strange error I encountered in the psql terminal

I am able to add new users using Postman with this model sequelize.define('users', { id: { type: S.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true ...

Two separate occurrences hold identical values

I'm encountering an issue where instantiating a class two times results in the second instance retaining parameters from the first instance. Here's a simple example: var Test = function() {}; Test.prototype = { bonjour: null, hello: { h ...

Error message: "An undefined index error occurred during an Ajax call to a

Path: homepage -> initiate ajax request to tester.php on PHP -> receive JSON data back to homepage. I am struggling to resolve this issue. Any help would be appreciated. AJAX Request: $.ajax({ url : "tester.php", ty ...

Scheduled Events and revising the date navigation in the calendar

https://developer.mozilla.org/fy-NL/demos/detail/html5-calendar/launch Among the codes provided, which specific code enables the use of the browser's back/forward buttons to change the day? I'm having trouble figuring it out. Additionally, do y ...

Changing the color of a button when it is disabled and then reverting back to its original color when

I am dealing with a pre-styled button where the disabled color does not appear disabled when the button is actually disabled. Unfortunately, removing the pre-styling is not an option. So, what I would like to do is have a conditional statement that says: ...

Using the TIMESTAMP data type in PostgreSQL and getting the most out of it

After saving a Luxon datetime value in a TIMESTAMP(3) type column in a postgres database, I encountered difficulty using it for various operations like converting time zones. Despite creating the object with the code snippet below: const { DateTime } = req ...

XSLT - Dividing table into three columns while maintaining continuity on the same page

I'm looking for a solution where I can display a long two column table in a three-column page layout. The table should seamlessly flow from one column to the next, maintaining its headers throughout. Current attempts show the entire table in just one ...

What is the process of creating and customizing popovers in Bootstrap 5 with jquery?

Is there a way to dynamically create and add content to Bootstrap 5 popovers using JavaScript or jQuery? In Bootstrap 3, I used the following method: $('#element').popover({ placement : 'left', trigger : 'focus', html: true } ...

AngularJS - managing checkboxes and dropdownlists

When I talk about the topic of depending on checkboxes from a dropdown list, I mention that I populate my dropdown list with data from the controller: @RequestMapping(value = "/all", method = GET) public List<Warehouse> findAll(){ return warehous ...

Insert code into the notes section of Pug

Need help inserting a script into a comment on Pug? I have two scripts that need to be added to my website: <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="http ...

Circular css3 animation originating from the center

Can someone provide an example of how to create a div rotating around a circle in CSS3 while the circle itself is already rotating? I attempted to use webkit frame, but it did not work correctly. If anyone has any examples or tips on achieving this effec ...

Ensure that the objection model aligns with the TypeScript interface requirements

I am currently working on implementing an API using express, objection.js, and TypeScript. I found a lot of inspiration from this repository: https://github.com/HappyZombies/brackette-alpha/tree/master/server/src Similar to the creator, I aim to have var ...

The outcome of data binding is the creation of a connected data object

I am attempting to link the rows' data to my view. Here is the backend code I am using: [Route("GetCarCount")] [HttpGet] public async Task<long> Count() { return await _context.Cars.CountAsync(); } ...

Is it possible to automatically close the modal by clicking outside of it

How can I make sure that my modal box only closes when clicking outside of it, and not when clicking on the buttons inside? I have a ref to the parent element that successfully closes the modal on click outside, but currently it also closes if I click on t ...

Stuck on using floats and fixed positioning in CSS, can't seem to crack the code

I am working on a mobile website project and I am in need of a fixed navigation bar that has a specific look... Unfortunately, my attempts at using fixed positioning to achieve this have not been successful. Instead of getting the desired result, all the ...

Is there a way to alter a modal's content in React without having to pass all the DOM elements as props?

I'm a newcomer to React and I'm currently working on creating a Two-Factor-Authentication modal-style UI for my project. The design of the modal is relatively simple: Title A message Modal Content (which can be an input box, one or more drop-d ...

Issue with clearing an array in JavaScript by reassigning it when accessed through a different reference variable

I discovered a workaround - by directly accessing the original array variable, I was able to achieve the desired result. Despite this, I am still intrigued by the reason behind this behavior. var array = ["hello"]; var testObject = {arr: array}; testObje ...

Strategies for making recursive calls to an API in the event of an expired access token in React Next.js without relying

Currently, I am in the process of constructing the client page using Next.js. To achieve this, I have implemented a function called handleSubmit that triggers an API call to generate a new product each time a user interacts with a designated button. Within ...

The paintbrush is whimsically altering its style on the canvas

My checkerboard has a unique feature where hovering over a tile will highlight it in yellow. Surprisingly, this action also turns the game border and the last white checker's border yellow as well. What's puzzling is that I never specified for t ...

How can you send an error message from Flask and then process it in JavaScript following an AJAX request?

So I have successfully created a Python backend using Flask: @app.route('/test/') def test(): data = get_database_data(request.args.id) # Returns dict of data, or None if data is None: # What should be the next step here? re ...