How can I customize an SVG using CSS when it's being accessed from another directory on my computer?

I find myself in a bit of a bind at the moment. Within my code lies an SVG depicting a map of the United States. Upon clicking on a specific state, the entire country fades away, revealing a separate SVG displaying only that state. Each of the 50 states is stored in its own folder and accessed like so:

//On Map Click
$(document).ready(function(){
  $('path').on("click",function(e){
    var state = "counties/"+$(this).attr('id') + ".svg";
    showState(state);
  });
});

<object style="width:auto; height:auto; " id="countyLevel" data="" type="image/svg+xml">
</object>

Now, I’m curious if it’s possible to apply CSS styling to these individual state SVGs. Perhaps something along the lines of path:hover?

Below you’ll find my showState() function:

function showState(stateFile) {
  $('.usa').fadeOut();
  $('.returncountry').fadeIn();
  $("#countyLevel").attr("data", stateFile);  
  $("#countyLevel").css('display', 'block');  
}

Answer №1

You have the ability to apply CSS properties to your SVG path just like you would with any other HTML element.

Utilize the fill property to alter the color inside the shape, and stroke property to change the color of the actual line path.

Check out this demonstration:

path:hover {
  fill: red;
  stroke: blue;
}
<svg width="190" height="160" xmlns="http://www.w3.org/2000/svg">
  <path d="M10 80 C 20 10, 65 0, 90 80 S 150 180, 180 80" stroke="black" fill="transparent"/>
</svg>

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

Unable to access hyperlinked text on WordPress sites

For a school project, I am in the process of developing a theme using WordPress to create a blog. This theme is not intended for sharing so all website design aspects are already pre-made. The issue arises when I attempt to access the blog.php file from ...

Passing props to child components in Next.js: A step-by-step guide

I am currently working with my index.js page, which incorporates a layout component: import Layout from "../components/layout"; export default function Home({posts}) { console.log(posts) return ( <Layout posts={posts}> & ...

Execute AngularJS code when a button is clickeddoubleValue

I am in the process of developing a dynamic page where users can add anywhere from 1 to n products effortlessly. Although I have been exploring AngularJS for several months, I find myself facing some challenges when conceptualizing this scenario. The HTM ...

There seems to be a lack of update in the fragment shader due to mouse movement

Struggling to incorporate the Voronoi shader from the Book of Shaders into three.js, I can't figure out why the mouse position isn't affecting my visible output. Even though I'm logging the mouse position and checking that the uniform value ...

What could be causing my website to extend beyond 100% width?

I've been working tirelessly on solving this issue for the past week, but unfortunately, I haven't had any luck finding a solution. The problem arose as I was designing a launch page for a program that my friend is developing. Despite setting the ...

Ways to enable scrollbars within a container when the content inside a code tag exceeds the visible area

How do I add scrollbars to a code element if the line of syntax is longer than the view? I've attempted using overflow: scroll, but it's not functioning as expected. As shown in the image, using overflow causes the browser to scroll horizontally ...

Unable to call Success function in JQuery AJAX request

Here is a simple JQuery ajax request I am working on: $.ajax("../ajax/data/items.json", { success: setContent, type: "GET", dataType: "json" }); function setContent(data, status, jqxhr) { alert("Hello!"); } The json file loads successfully with a 200 r ...

What is the best way to limit the range slider before it reaches its maximum point?

I am currently utilizing angularjs to stop a range slider at 75%, however, the method I am using is not very efficient and is not working as desired. Is there anyone who can provide guidance on how to achieve this? Please note: I want to display a total ...

Having trouble retrieving the routeName as it consistently returns empty

I attempted to extract the routeName from the URL in order to apply a different class to the body's layout when on the /Category page. https://i.stack.imgur.com/J0hsp.png @{string classContent = Request.QueryString["routeName"] != "/ ...

Issue with static resource fetching when referencing a .js file within an HTML document while using Flask

My HTML file loads my OpenLayers JavaScript file (which displays a map frame) when opened directly. However, when running the HTML from my Flask python app, the JavaScript file/object fails to load (resulting in no map display, just some heading text). I ...

Adding new element to 2D array

I'm currently facing an issue while attempting to insert strings into a 2D array using Google Apps Script. The behavior I'm encountering is quite peculiar and I'm struggling to comprehend it fully. Below is a simplified version of the code t ...

Selenium's WebDriver getAttribute function can return an object of type "object",

In my selenium script, I aim to extract text from table columns following the cell with the specified value. Although the script functions, I encounter an issue where getText() returns [Object Object] in my node.js console. I have attempted various method ...

Utilizing JavaScript to iterate through objects retrieved via Ajax calls

Recently, I've been diving into the world of Javascript and delving deep into AJAX. Utilizing Vanilla JS along with simple AJAX, my current task involves fetching a user object from a URL based on the user's input ID. Despite attempting to use .d ...

Steps to displaying the result

new Vue({ data: { folders : [{ name : 'folder1', isActive : 1, }, { name : 'folder2', isActive : 0, }, ] } } }) Is there a way to access the active val ...

When using jQuery and AJAX together, it seems that the POST method is returning

Currently experimenting with Cloud9. The current project involves creating an image gallery. On the initial page: There are a few pictures representing various "categories". Clicking on one of these will take you to the next page showcasing albums fro ...

Trouble with NodeJS async/await - navigating through function arguments

Currently, I am attempting to perform web scraping using a particular library while implementing Node's async/await method in my code. Within the function named 'sayhi', I have created a variable 'page' and encountered an issue wh ...

Ending asynchronous tasks running concurrently

Currently, I am attempting to iterate through an array of objects using a foreach loop. For each object, I would like to invoke a function that makes a request to fetch a file and then unzips it with zlib, but this needs to be done one at a time due to the ...

What is a global variable used for in JavaScript?

Here is the code snippet that I am currently working on: $(".link").each(function() { group += 1; text += 1; var links = []; links[group] = []; links[group][text] = $(this).val(); } ...

Using AJAX and PHP to submit a form and receive a success response text without the need to reload the page

Greetings, fellow coders! I'm excited to be posting my first question here. As a beginner in the coding world, I've already made some progress thanks to the helpful answers I've found on this platform! Today, I am encountering an issue with ...

The white-space property disrupts the normal width of elements

My initial goal was to stack two elements together side-by-side, both taking the full available height. One element has a fixed width of 200px with an image (70px wide) inside, while the other element should only fit one line of text, clipping any overflow ...