How about this: "When you hover over a link, watch as the

I designed an SVG map and I'm trying to change the color of the targeted areas on the map when hovering over a link. Currently, when I hover over an area on the map, the link receives the underline class from jQuery and the CSS hover effect changes the fill color on the map. However, I am facing an issue where when I hover over the link, the SVG area does not change its fill color as intended. I am seeking assistance to resolve this problem.

Here is an example of what I am aiming for:

CSS:

path:hover{ fill: #FEFF7F !important; }
.svgcolor{ fill: #FEFF7F !important; }
.underline{ font-weight:600; text-decoration: underline; }

HTML:

<li id="maplink01"><a href="#" title="Text" target="_blank">Text</a></li>

SVG:

<a xlink:href="http://www.google.com" target="_blank">
<linearGradient id="SVGID_5_" gradientUnits="userSpaceOnUse" x1="393.5261" y1="226.1426" x2="446.614" y2="226.1426">
<stop offset="0" style="stop-color:#E7ECF9" />
<stop offset="1" style="stop-color:#C4CFE1" />
</linearGradient>
<path id="mapimg01" style="fill:url(#SVGID_5_);stroke:#000000;stroke-width:0.5;stroke-miterlimit:10;" d="M440.333,216... /></a>

jQ:

//This works fine:
            $("#mapimg01").hover(function () {
                $("#maplink01").addClass('underline');
            }, function () {
                $("#maplink01").removeClass('underline');
            });
//This does not work:
            $("#maplink01").hover(function () {
                $("#mapimg01").addClass('svgcolor');
            }, function () {
                $("#mapimg01").removeClass('svgcolor');
            });

Answer №1

After some trial and error, I finally found a solution to the issue at hand. Instead of using add-/removeClass on the SVG element, I had to resort to utilizing setAttribute("style", "fill:..."). Although I attempted this approach earlier, it became apparent that I couldn't use jQuery's $ sign and instead needed to employ the entire document.getElementById method. Why these methods yielded different results remains a mystery to me. The solution I settled upon looks like this:

var mapImg = document.getElementById("mapimg01");

$("#maplink01").hover(function () { 

mapImg.setAttribute("style", "fill:#FEFF7F; stroke:#000000;stroke-width:0.5;stroke-miterlimit:10;");
}, 

function () {


mapImg.setAttribute("style", "fill:url(#SVGID_5_); stroke:#000000;stroke-width:0.5;stroke-miterlimit:10;");
});

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

What is the best way to prevent card-group from wrapping on smaller screens?

What is the best way to ensure that cards in a card-group stay adjacent to each other on smaller screens? Currently, they are grouped together on larger screens but appear separated on smaller screens. I am currently hiding the additional cards on smaller ...

The magic of JQuery DataTables

I am a beginner in jQuery and I have recently implemented Datatables in my grid. However, I encountered an issue where the info bar at the bottom of the jQuery data tables is not displaying as intended: see image here My goal is to show the first entries ...

What is the best way to maintain the original font color even after hovering over it?

Hello everyone, I'm having trouble maintaining the white color on hover. When I move the cursor to the next element inside the hover element, the font color changes. I've tried various methods to keep the desired color but nothing seems to work. ...

Determine distinct items in an array that match a predefined criteria

I have a list of objects with two keys, img1 and img2. I need to identify unique objects based on the values of img1, while also retaining the corresponding value of img2. This is the current code I am using: const imgs_arr = [ ...new Set( inpu ...

I'm wondering why my typography components display correctly on my local host, but not on my aws server. Any insights on

I've encountered an issue with the typography component I'm using for my headings. When I publish the website, the headings do not render properly and the styles are not applied correctly. Strangely, everything looks fine during npm run dev, but ...

How can I retrieve the data passed in a post request using Azure Functions and JavaScript?

I have a JavaScript Azure function that takes a context and request as parameters: function(context, req) It's easy to retrieve data from a GET request using the req object. For example, if I pass name=test in the URL, I can retrieve it in my code l ...

JavaScript that is subtle and lacks function parameters

Suppose you have: <div id="data" onclick="handleData(1)">Datum 1</div> and you prefer late binding instead: <div id="data">Datum 1</div> <script> $("#data").click(function() { handleData(1); }) </script&g ...

Exploring nested styles within Less to style both parent elements and their children

Is there a way to apply styles to an element and its descendants? For example: .datepick-month-header, .datepick-month-header select, .datepick-month-header input { background-color: black; color: #fff; font-weight: bold; } In some less documentation, I ...

Can you identify the issue with this particular JSON parsing function's reviver?

I am trying to parse a JSON string with a reviver function to only include specific properties. Here is my code snippet: const whitelist = ['prop1', 'prop2', 'result']; const reviver = (key, value) => { if (whitelist.i ...

Step-by-step guide to creating an Angular HTML template when a button is clicked:

I'm attempting to design an HTML template in order to export it as a PDF File, however, I'm unsure how to accomplish this using Angular's engine; Essentially, what I want to achieve is to construct this template when I click a "Download As ...

Reloading the Angular application returns it to its initial state

I am currently developing a Dashboard using AngularJS. I am utilizing ui.router to generate states from a JSON file named `pages.json`. Even though my app is functioning properly, I am facing an issue where refreshing the page with the URL "localhost:#/ma ...

Utilizing a shared component in both a child and parent within a Vue application

I am currently working on a VueJS project, where I have parent and child components within one view. Both components are using the same component called deleteModal. The issue arises when I try to trigger the modal from the child component - it ends up tr ...

An unusual phenomenon in the controller hierarchy causing issues with accessing the parent scope in AngularJS

Recently delving into the world of angularjs, I encountered something perplexing that seemed like a glitch in the controller hierarchy when it comes to accessing the parent scope. Here are two code snippets that I believed should both function properly bas ...

An Animated Way to Shut Modal Window

Below is the code snippet used to trigger the modal: <div className={classes.modal}>{props.children}</div> .modal { width: 90%; background-color: white; z-index: 30; position: fixed; top: 20vh; left: 5%; border-radius: 12p ...

Tips for creating dynamic row styles in a fluid table layout?

Could someone help me figure this out? I'm not familiar with JavaScript and need assistance as I've never created it, only edited existing scripts. I have a layout with two tables side-by-side. On mobile devices, the second table is pushed below ...

What is the best way to reset a timer in reactjs after using clearInterval()?

I have a timer set up, and when I call clearInterval(this.temporizador), the timer stops. Now, when I call resetState(), my goal is to restart the timer from 0 but it's not working as expected. Can anyone spot what I might be doing wrong? componentWi ...

Having trouble retrieving a remote JSON link from a local HTML file in Google Chrome

Having trouble fetching a JSON from the following link: [https://www.nseindia.com/api/equity-stockIndices?index=NIFTY%2050]. When accessed through a browser, the JSON is displayed on screen as expected. However, attempting to fetch this link from JavaScrip ...

Using Vuetify to Create Dynamic Property Bindings

I have a form with a variety of dynamic input components, such as v-selects and v-text-fields, that users need to fill out. However, I am having trouble figuring out how to retrieve the values from these inputs once the form is submitted. Here is an exampl ...

When capturing photos using h5 on iOS devices, the browser may experience a flash back issue

I have been searching Baidu and Google for a while now, but I still haven't found a solution. Here is the specific issue: When using h5 to take photos on iOS systems, the browser flashes back. HTML Code <img src="xxx" onclick="sta ...

I attempted to install react-compare-image using npm, but encountered an error stating that the dependency tree could not be resolved

PS D:\skinprojecct> npm install --save react-compare-image npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: [email protected] npm ERR! Found: [email protected] npm ERR! node_mod ...