Struggling to find a solution for altering font color with jQuery while creating a straightforward menu

I'm having trouble changing the color of the active button pressed to "color:white;"... Here is the link to the jsfiddle: http://jsfiddle.net/wLXBR/

Apologies for the clutter in the file, my css is located at the bottom of the CSS box (Foundation code first).

In the jsfiddle, when you press a button that has a sub-menu, it turns green. I want the text in the green box to turn white while it's active just like it becomes green. I've tried everything (or so I thought)... Any suggestions?

EDIT: Didn't realize code needed to be included...

CSS:

/* Menu styling */

.side-nav {
    padding: 0px;
}

.side-nav > li {
    border-bottom: 1px solid #393939;
    margin: 0px;
}

.side-nav > li > a {
    border-bottom: 1px solid #181818;
    padding: 10px;
    color: #a7a7a7;
    font-size: 13px;
}

.side-nav > li > a:hover {
    color: #eaeaea;
}

.is-sub {
    background: #2d2d2d;
    margin: 0px;
    display: none;
}

.is-sub > li {
    background: #2d2d2d;
    margin: 0px;
}

.is-sub > li a {
    padding: 10px;
    font-size: 12px;
    color: #a7a7a7;
}

.has-sub.active {
    background: #48AC40;
}

Javascript

    jQuery(document).ready(function($) {

        $('.has-sub').click(function() {
            $(this).toggleClass("active");
            $(this).children('.is-sub').slideToggle('slow');
            return false;
        });
    });

HTML:

<ul class="side-nav">
            <li>
                <a href="#">Dashboard</a>
            </li>
            <li class="has-sub">
                <a href="#" class="">Posts</a>
                <ul class="is-sub">
                    <li>
                        <a href="#">Add a new Post</a>
                    </li>
                    <li>
                        <a href="#">Edit Posts</a>
                    </li>
                </ul>
            </li>
            <li class="has-sub">
                <a href="#" class="main">Manage</a>
                <ul class="is-sub">
                    <li>
                        <a href="#">Categories</a>
                    </li>
                    <li>
                        <a href="#">Pages</a>
                    </li>
                    <li>
                        <a href="#">Users</a>
                    </li>
                </ul>
            </li>
            <li>
                <a href="#">Logout</a>
            </li>
        </ul>

Answer №1

$(document).ready(function() {

    $('.has-sub').click(function() {
        $(this).toggleClass("open");
        $(this).children('.sub-menu').slideToggle('slow');

        if ( $(this).hasClass("open") ){
            $("a", this).eq(0).css("color", "black");
        } else {
            $("a", this).eq(0).css("color", "");
        }
        return false;
    });
});

Answer №2

Here is an alternative method you can try:

$('.sub-menu').click(function() {
    $(this).toggleClass("active");
    $(this).children('.submenu-items').slideToggle('slow');

    if ( $(this).hasClass("active") ){
        $(this).find('a').css("color", "#00ff00");
    } else {
        $(this).find('a').css("color", "#cccccc");
    }
    return false;
});

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

How to create a full-width and full-height background image in a div using CSS

When using a bootstrap template, I encountered an issue with adding an image as a background. Here is the CSS code: background:url(../images/banner.png) no-repeat; background-size: 100%; The problem with these CSS rules is that the image gets clipped and ...

Is it possible to transfer data to the next page by clicking on a table row?

I'm facing an issue with a non-clickable table. When a row is clicked, I want to navigate to a detail page from the table while passing the id value in the process. I know that I need to create an href attribute on the table row and somehow transfer ...

Encase a group of div elements with identical styling

Looking for a solution on how to programmatically wrap a set of divs with a parent div based on a certain class match. Any suggestions or ideas on how to achieve this? ...

Close the Bootstrap modal upon clicking the "Ok" button in SweetAlert2

I need help with closing my Modal Bootstrap after clicking the Ok button in SweetAlert. I've attempted various methods, but none of them have worked so far. I'm not sure if I'm implementing it correctly, but I've utilized JQuery for thi ...

disappearance of background image at bootstrap breakpoint

Hey there, I'm diving into a new journey in web design and I've encountered my first newbie issue. I'm attempting to place an image and text side by side on my web layout, but as soon as the breakpoint hits below 560px, the image disappears ...

The functionality of my JQuery validation plugin seems off when it comes to handling checkbox inputs

I created a versatile validation plugin that accepts a function to check input validity, along with callbacks for valid and invalid cases. The plugin consists of two functions: '$.fn.validation()' to attach validation logic and success/failure ca ...

Illustrating a fresh DOM element with intro.js

I am currently utilizing intro.js to create a virtual 'tour' for my website. As I aim to provide a highly interactive experience for users, I have enabled the ability for them to engage with highlighted DOM elements at different points in the tou ...

What is the best way to trigger a modal on Bootstrap using a JavaScript/jQuery function?

I encountered an issue while attempting to call a bootstrap modal using a simple button or link, which may be due to a conflict with my select2 plugin (although I am uncertain). I tried appending the button to the select2 dropdown but it doesn't seem ...

Navigate to the following fieldset using jQuery modal after receiving a response code through AJAX

I have a GET AJAX call being sent to Mailchimp from a JQuery modal using the following scripts. How can I automatically navigate the user to the next fieldset if the server response is successful? Should this be done in the initial JavaScript or in the PHP ...

Reveal Password Feature in Angular After User Click

I am working on an inventory page that includes a password field. My goal is to initially hide the password and display it as points (****) instead. I would like the password to be revealed either when clicked or through a pop-up. JS var retrieveCert ...

Why is this strange red dot character 🔴 appearing in the URL in FireFox?

While browsing the web, I came across a website with a strange red blob in the URL. It's something I've never seen before. What could possibly be the reason for this unique feature?! Note that you have to visit the page to see it for yourself. T ...

Building a Modal in React and Triggering it for Display, followed by Making an AJAX Request upon Submission

Check out my CodePen snippet (minus delete ajax request): http://codepen.io/martincarlin87/pen/KzPWOw I've been diving into the world of React recently, and I'm working on converting a page in my web application to utilize React instead of just ...

Deciphering assorted outcomes from the Google feed API

Could anyone provide me with an example of parsing a mixed feed result from the Google API using both XML and JSON? I'm attempting to access the enclosure element of the feed, but it appears that the JSON result does not include it! Thank you in advan ...

After changing the page, the Facebook JS SDK fails to function properly when using JQueryMobile

I'm facing an issue with my webapp that utilizes jQuery Mobile for full ajax navigation. I have initialized the Facebook SDK at the pageinit event in jQueryMobile (called on each page). jQuery(document).on('pageinit', function (event) { ...

tips for selecting various API requests based on the selected drop down menu choice

Hey there! I'm looking to enhance my login page by adding a feature that allows users to select from a dropdown menu with different options. Each option will be linked to a specific API, and based on the API response, the user's ability to log in ...

What is the best way to eliminate a specific set of characters from a string using TypeScript?

Imagine you have the following lines of code stored in a string variable: let images='<img alt="image1" height="200" src="image1.jpg" width="800"> <img alt="image2" src="image2.jpg" height="501" width="1233"> <img alt="im ...

Struggling to insert a high-quality image into inline divs of exactly 33.3333% width

After creating 3 inline divs, each taking up 33.333% of their parent width, I encountered an issue while trying to insert images into them. Whenever the image exceeds the 33.333% width of the div, it overflows outside the container. My goal is to make the ...

Tips for limiting the .click function to only the initial image in order to prevent loading all images within the container

I am facing a situation where multiple images are being returned into a specific div, which is working as intended. <div class="go" id="container"></div> Upon clicking on an image, it is loaded into a modal popup. However, instead of capturin ...

Move the last specified elements to the beginning of the array

I have an array let colorsArr = ["red", "green", "purple", "pink", "black"]; Is there a way to retrieve a specific number of elements from the END and move them to the BEGINNING of the array? Desired result: example 1: //move last 3 elements let expec ...

Extract the content of an element and incorporate it into a script (AddThis)

HTML: <div id="file-section"> <div class="middle"> <p class="description"> Lorem ipsum... </p> </div> </div> jQuery: var share_on_addthis = { description: $('#file-section ...