Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:46:20+00:00 2026-06-14T10:46:20+00:00

I have made some menus which have items that expand when the user clicks

  • 0

I have made some menus which have items that expand when the user clicks on them. Whenever the menu item is expanded, the headline of the item is supposed to change style using .css()

Since the menus are styled differently, there is an if statement within the script, that checks whether the user clicked a menu2 or a menu3 item to apply the appropriate style when it expands.

EDIT: The problem is that the styling changes do not apply as they should.

I replaced $this to $(this) as Sushanth suggested, but the problem persists, check the example (updated)

Code:

<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function() {

    $(".toggle-trigger").click(function() {
        $(this).parent().nextAll('.toggle-wrap').first().slideToggle('slow','swing'); 


      if( $(this).is("menu2 .headline a")) {
            $(this).toggle(
                function(){
                $(this).parent().css("background-color","rgba(0, 0, 0, 0.1)",'border', 'solid 2px #009e0f');
                $(this).css("color","#009e0f");
                },
                function(){
                $(this).parent().css("background-color","#009e0f","border",'solid 2px #009e0f');
                $(this).css("color","#ffffff");
                }
            );
        }

        else if( $(this).is("#menu3 .headline a")) {
            $(this).toggle(
                function(){
                $(this).parent().css("background-color","rgba(0, 0, 0, 0.1)",'border', 'solid 2px #f7b50c');
                $(this).css("color","#f7b50c");
                },
                function(){
                $(this).parent().css("background-color","#009e0f","border",'solid 2px #f7b50c');
                $(this).css("color","#ffffff");
                }
            );
        }



    });
});

});

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:46:22+00:00Added an answer on June 14, 2026 at 10:46 am
    $this
    

    supposed to be // In the if statements if( $this.is("menu2 .headline a"))

    $(this)
    

    Or cache var $this = $(this)

    UPDATE

    I see two more problem with the code …

    • Missing # sign in this line if( $(this).is("#menu2 .headline a"))
      Second problem is in the way you are assigning the CSS properties..

      .css(“background-color”,”#009e0f”,”border”,’solid 2px #f7b50c’)

    You have multiple properties .. So the properties are supposed to be in the form of a map with key:value pairs..

    .css({
           "background-color": "#009e0f",
           "border": 'solid 2px #f7b50c'
     });
    

    Check Fiddle

    No need to encase your code in both $(window).load() and DOM Ready handler.. One is sufficient.
    Also there seems to be a problem with the toggling

    UPDATE

    Optimized without using toggle

    $(document).ready(function() {
        $(".toggle-trigger").click(function() {
            // cache the div next to anchor
            var $div = $(this).parent().nextAll('.toggle-wrap').first();
            $div.slideToggle('slow', 'swing');
            // cache $(this)
            var $this = $(this);
            var background = '';
            var color = '';
            // Checking id the div has no class called hidden
            if (!$div.hasClass('hidden')) {
                // then add the class to the div
                $div.addClass('hidden');
                background = "rgba(0, 0, 0, 0.1)";
                // closest ancestor of the link cliked is menu2
                if ($this.closest('#menu2').length) {
                    color = "#009e0f";
                }
                // closest ancestor of the link cliked is menu2
                else if ($this.closest('#menu3').length) {
                    color = "#f7b50c";
                }
            }
            // if the div has a class hidden then 
            else {
                // Remove the hidden class
                $div.removeClass('hidden');
                color = "#ffffff";
                if ($this.closest('#menu2').length) {
                    background = "#009e0f";
                }
                else if ($this.closest('#menu3').length) {
                    background = "#f7b50c";
                }
            }
            // set the background  and color based on conditions
            $this.parent().css("background-color" , background );
            $this.css("color" , color);
        });
    });​
    

    Updated Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one sliding menu in which I slide some div. In that after
I have made some changes in one perforce client, but haven't submitted them. I
I have recently made changes in some code that makes a char name field
I have made some progress with the DataList and UserControl this morning but I
I have made some screenshots of my website, and in internet explorer 6 my
I have made some screenshots of my website, and in internet explorer 6 my
I have a datagrid in C#.net I have made some columns in grid editable.
I posted a thread about this earlier and have made some progress but now
I'm using Pex and Moles 0.94.51023.0 with VS2010 SP1. I have successfully made some
i have made columns in some of the tables encrypted in sql server 2008.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.