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

Ways to display a series of images side by side to fill the entire width consistently

I need a solution to display multiple images in a box, all set to the same height and width of 154px x 125px. While this can be achieved, the issue arises when there isn't enough space for the final image, leaving blank areas. Is there a way to make t ...

CSS an act of misbehavior

I have noticed some unusual behavior on my website page. My website design can be viewed at Also, here is the same design in CMS format Please pay attention to the section under <div class="godelpage"> <a href="http://ryugaku.babonmultimedia ...

Custom font not displaying on Chromecast receiver app

I have followed the specified steps to incorporate a custom font into an html canvas text field. Interestingly, the font displays correctly when accessed on the Desktop Chrome browser, but on the Chromecast receiver application, the font fails to load. Wha ...

What is the best way to ensure my <h5> element fits perfectly inside its parent div vertically?

Currently facing an issue with finding a solution to my problem. The challenge involves having a header tag nested within multiple divs. Below is the structure: <div class="card"> <div class="layout-left"> <div class="card-header"> ...

jQuery form experiencing a small problem

I need assistance with a form that has four fields. Specifically, I am looking for a solution where the visibility of the County field is dependent on the selection made in the Country field. When UK is chosen in the Country field, I want the County field ...

The dimensions of the bottom border on left floating divs are inconsistent

One of my designers frequently uses a particular technique in various designs, and I'm struggling to determine the most effective way to style it with CSS so that it fluidly adapts (as it will be incorporated into a CMS). Essentially, when implementi ...

Ways to boost CSS transform with GPU acceleration

Is there a way to ensure smooth animations by utilizing GPU acceleration for CSS transforms in browsers? When does this acceleration occur and how can it be forced? For more information, check out this article ...

Incorporating Card Layouts with SwiperJS

Reference Code: See Example on Code Sandbox I am looking to implement a swiper that allows for navigating one slide at a time with a layout where slidesPerView: "auto" and fixed width are used. The first two slides behave as expected, but the la ...

Which method proves to be quicker: storing data on a DOM element with a class attribute, or using jQuery data?

In my application, I have a render loop that constantly updates the appearance of several DOM elements based on data fetched from an external source. Each element needs to have presentational classes applied according to this data. For example: var anima ...

Using ajax to submit a form in CodeIgniter and displaying the returned data in a table view

When I submit a form on the view page, I want the form data to be saved in a database table and displayed without refreshing the page. Below is my view code: <div class="col-md-12"> <div class="row"> <div> <table class="table table-s ...

Calculate values dynamically when styling material-UI components

Utilizing the Material-UI style to define the class. Now, I aim to adjust the height based on the browser's width. Is there a way to dynamically calculate this within makeStyles() or implement a workaround? This snippet below represents what I am t ...

Issue with undefined bindingContext.$data in IE9 on knockout binding handler

I'm attempting to create a unique binding handler that applies role-based access to fields on a page. This custom handler uses the values of other observables from the viewModel to enable or disable input controls based on certain conditions. However ...

Error occurs when utilizing jQuery ajax on Samsung devices

Encountering a puzzling issue with jQuery (3.3) ajax. Even after simplifying the code to its bare minimum, the reason behind this problem remains unclear. Using an HTML form to capture 'Submit' and trigger a PHP script via ajax. The ajax call is ...

When you hover over them, chips transform their color

I am currently using a chip in my code and I would like to change its color when the mouse hovers over it. I attempted to achieve this by using: hover:{ backgroundColor: 'red', } In addition, I incorporated const StyledChip ...

The self-made Ajax call for deleting an action in yii2 is not functioning as expected

Trying to implement a custom Ajax request in my Yii2 application for performing a delete action. The code snippet for the action column button: 'delete' => function ($url, $model) { return '<span class="glyphicon glyphicon-trash ...

Failed to send JSON data to WebMethod

I am encountering difficulties while attempting to send JSON to a WebMethod. I have provided the method I am using below. If there is a more efficient approach, please advise me. My goal is to store the JSON object in a database. JavaScript function TEST ...

Searching for the index of a nested array in jQuery using JSON

I am currently working on developing an image uploader using Codeigniter3 along with jQuery and Ajax. Problem: I am facing difficulty in understanding how to locate the index of the array received from the ajax response shown below. Here is the data retu ...

Popup Triggered by a JavaScript Timer

Seeking assistance in creating a straightforward timer-based popup. Once the timer hits 10 seconds, the popup should become visible. The timer should pause or stop, and after clicking the 'ok' button on the popup, the timer should reset to 0. Can ...

When the add button is clicked, I would like to implement a feature where a checkbox is added

When the user clicks on the link "출력하기", I want the web page to add a checkbox to all images. I wrote this code, but it's not working. Can anyone help me? This is my JS: $(document).ready(function(){ $("#print").on('click', fu ...

Unable to set two image layers on HTML canvas to display in different colors as anticipated

I am facing a challenge with stacking 3 images: an outline, a white base, and a pattern that is also white. My goal is to layer these images where the base is at the bottom with one color assigned to it, the pattern is stacked on top with a different color ...