Modify the color of the 'li' element that shares the same attribute value as the current page

I am in the process of creating a website for an architecture company. Currently, I am working on a specific page which you can find here: . Each project falls into a category such as residential, commercial, or education. On each project page, I include the following code to indicate what type of project it is:

<div id="projects" data-cat="commercial">

Using Javascript / jQuery, I extract this attribute value like so:

var cat = $("#projects").attr('data-cat');

When I use alert(cat), it displays the correct project type.
I have navigation menu links that correspond to each project type using the data-highlight attribute:

<ul id="sub">
    <li data-highlight="commercial">
        <a href="http://www.hla.co.za/projects/#commercial">Commercial</a>
    </li>
    <li data-highlight="residential">
        <a href="http://www.hla.co.za/projects/#residential">Residential</a>
    </li>
    <li data-highlight="education">
        <a href="http://www.hla.co.za/projects/#education">Education</a>
    </li>
</ul>

I am trying to change the font color of the current project type by running the following script:

$("li[data-highlight='"+cat+"']").css( "color", "red" );

Unfortunately, the color does not change as expected. Strangely, when I attempt something else like removing the element:

$("li[data-highlight='"+cat+"']").remove();

It works perfectly fine. I'm puzzled by this behavior and would appreciate any assistance you can provide.

Answer №1

The color being set for the li element is being overwritten by the inline style of the inner a element.

To prevent this, you can simply use

$("li[data-highlight='"+category+"'] a").css( "color", "red" );
and your desired styling will be applied correctly.

Answer №2

Upon reviewing the page you provided a link to, it appears that the issue lies within your style.css file. Specifically, there is a section of code that reads as follows:

.nav a {
    text-decoration: none;
    color: white;
}
The problem arises from the fact that the color applied to the "a" element takes precedence over the color set for the "li" element. As a result, any changes made to the color of the "li" are not being reflected. To resolve this issue, you will need to either remove the styling for the "a" element or apply the color change directly to the "li" element.

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

The CssDependency dependency type in vue-cli@3 does not have any module factory available

After using vue-cli@3 to npm run build The system showed an error message: No module factory available for dependency type: CssDependency I have extensively searched for related solutions, but they all pertain to Angular. I also attempted the following ...

Utilize Javascript/Jquery to categorize JSON data based on the days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

A function is provided below that retrieves data for a chart. The output produced by this function is structured as follows: [Object { date=Date, value=112, volume=1469}, Object { date=Date, value=124, volume=539}, Object { date=Date, value=114, vo ...

Calculating values within dynamically generated elements requires utilizing JavaScript to target and extract the

I am working on creating input fields inside an HTML table using Vue.js. On click of a button, I want to perform some calculations based on the input values. However, it seems that the calculations are not happening as desired. What I have attempted so fa ...

"Stay entertained with a captivating animated gif that appears when you refresh the

I just implemented this code snippet to enable animated gifs to work after refreshing a webpage. It's functioning properly in Chrome, Safari, and Internet Explorer except for Firefox. Can someone please offer assistance? jQuery: $(img).css("backgrou ...

Seeking assistance with my basic JavaScript/jQuery programming

I have a navigation menu link with an ID #navigation li a and a description with a class .menu-description. I would like to update the class from .menu-description to .menu-descriptionnew whenever a user hovers over #navigation li a. Here is my current jQ ...

There seems to be a problem with completing the Axios post request in the Javascript Front

My front-end, built with React, is having trouble retrieving data from a third-party API through my Node.js backend. Despite using axios.post request, the response remains undefined and I'm stuck at handling the asynchronous request. It appears that t ...

PHP code to display or conceal tables

I am facing a challenge in my PHP code where I need to hide a table based on a specific condition. Despite trying to use CSS for this purpose, it seems that CSS does not recognize the if condition and always overrides it. I am looking for a solution that ...

Is the background color not being implemented?

I'm struggling with an issue - I can't seem to apply a background color to my top bar. I've tried multiple times but it's still not working. Could you please help me fix this error? https://i.sstatic.net/mzJIo.png Below is the code fo ...

Identifying button clicks using selenium-wire in Python

Is there a way to use selenium-wire in python to capture a full screenshot of a web page and save it as a png file after clicking the submit button? Despite seeing a popup message saying "taking screenshot!!", the actual screenshot file is not being saved ...

Tips for implementing conditional app.use() in nestJS backend strategies?

Trying to incorporate helmet into my nestJS application. I also require the inclusion of graphqlUploadExpress. How can I properly utilize the usesUpload condition to implement either helmet alone or along with upload? import { NestFactory } from '@nes ...

.Certain IDs on a website may not respond to clicks due to various reasons

I have created a JavaScript file to automatically click a button on a website using a Chrome extension. When testing the code using Chrome Script snippet, I noticed that it worked for some IDs but not for others. Can someone please help me with this issue? ...

Invisible floating div above applet within iframe on Chrome not displaying

When loading an external page with an applet in an iframe, I encountered an issue with displaying a floating div above the iframe. Setting position relative or absolute on the containing divs and a higher z-index on the floating div worked in Firefox, but ...

I am having trouble with the dropdown button in my Bootstrap - it just won't seem

I've exhausted all YouTube tutorials and followed the entire Bootstrap documentation, but I still can't seem to get it to work. Please, someone, help me figure out why it's not functioning properly :( This is the basic HTML bootstrap code I ...

Initiate an animation in Wordpress once the entire page has finished loading

Recently, I incorporated some "raw html" elements with animations into my WordPress site. However, the issue I'm facing is that these animations kick off as soon as the page loads, without waiting for the preloader to complete and display the actual c ...

What's the issue with reducers receiving a Function instead of an Object in Redux?

I encountered an error in my react and redux project but I am unable to figure out how to fix it. Here is the error message: The reducer received unexpected type "Function" as the previous state. It was expecting an object with keys: "posts", "sidebar" ...

Combining arrays using value comparison in Google Analytics and MongoDB

Help me out, Stack! This job is driving me crazy. Here's what I'm working on: Using the Google Analytics NodeJS SDK, I'm retrieving data about the most visited pages of my website. By leveraging Google's user-friendly URLs (slugs), I se ...

Issues with Angular HTML failing to load correctly

Greetings, I am seeking assistance with an issue that has been challenging me for quite some time. As a newcomer to Angular JS, I may be overlooking something obvious. Here is the scenario: I am utilizing ng-repeat on a directive in the following manner: ...

Having trouble resolving a setInterval problem with JavaScript?

Yesterday, I learned about the setInterval function which allows me to execute a task or function after a specific amount of time. While I have successfully implemented the interval in my code, it keeps adding new lines with the date each time. What I re ...

Jquery UI slider malfunctioning would require troubleshooting

I am currently working on implementing slider functionality with jQuery. The slider is visible and functioning properly in terms of taking in values, however, the values are not displayed on the screen as expected. I have used console.log to track the ui.v ...

Python script for connecting to mWebSocket Server using WebSocket client

I am in the process of developing a WebSocket client using python that establishes a connection to an Arduino Uno via an Ethernet cable every second. The Arduino Uno is equipped with a WebSocket server using the mWebSocket library. The operational code for ...