What is the best way to reset an element to its default state upon clicking a div using jQuery?

I am working on a navigation menu that has a toggle for the drop-down feature. The default state of the toggle is blue, but when the drop-down menu is activated, it turns red.

The issue I am facing is that when I click on one of the li items in the menu, it does not revert back to its default blue state; instead, it stays red.

My question is: How can I make the menu toggle go back to its default state of blue when an li item is clicked? Additionally, I would like it to rotate and change color back to blue when returning to the default state.

For reference, here is a JSFiddle link: https://jsfiddle.net/61g4std1/9/

Thank you for your assistance!

/*===================
---- Menu Rotate ----
===================*/
$(document).ready(function() {

var rotation = 0;
$('#menutoggle').click(function() {
    rotation += -180;
    $(this).css({'-webkit-transform' : 'rotate('+ rotation +'deg)',
             '-moz-transform' : 'rotate('+ rotation +'deg)',
             '-ms-transform' : 'rotate('+ rotation +'deg)',
             'transform' : 'rotate('+ rotation +'deg)'});
});

$('#menutoggle').click(function() {
$(this).toggleClass('rotated');
});

$('#menutoggle').click(function () {
   $(this).toggleClass("hov");
});

})

/*=====================
---- Menu Dropdown ----
=====================*/
$(function() {
    $("#menutoggle").click(function () {
        $(this).toggleClass("#mobilemenu");
    });


    $('#menutoggle').click(function(){
        $('#mobilemenu').toggle('blind');
    });


    $("#mobilemenu li").click(function() {
        $("#mobilemenu li").removeClass('hover');
        $(this).addClass('hover');
    });

    $('a#hide, a#hide2').click(function(){
        $('#mobilemenu').hide();
    })


});

Answer №1

If you want to revert your #menutoggle element back to blue, you can either manually fire your event handler or attach the necessary classes to it. You can check out an updated jsfiddle Here.

/*===================
---- Menu Rotate ----
===================*/
var rotation = 0;
$(document).ready(function() {

$('#menutoggle').click(function() {
    rotation += -180;
    $(this).css({'-webkit-transform' : 'rotate('+ rotation +'deg)',
                 '-moz-transform' : 'rotate('+ rotation +'deg)',
                 '-ms-transform' : 'rotate('+ rotation +'deg)',
                 'transform' : 'rotate('+ rotation +'deg)'});
    $(this).toggleClass('rotated');
    $(this).toggleClass("hov");
});

})

/*=====================
---- Menu Dropdown ----
=====================*/
$(function() {
    $("#menutoggle").click(function () {
        $(this).toggleClass("#mobilemenu");
    });


    $('#menutoggle').click(function(){
        $('#mobilemenu').toggle('blind');
    });


    $("#mobilemenu li").click(function() {
        $("#mobilemenu li").removeClass('hover');
        $(this).addClass('hover');
        rotation += -180;
        $('#menutoggle').css({'-webkit-transform' : 'rotate('+ rotation +'deg)',
                 '-moz-transform' : 'rotate('+ rotation +'deg)',
                 '-ms-transform' : 'rotate('+ rotation +'deg)',
                 'transform' : 'rotate('+ rotation +'deg)'});
        $('#menutoggle').toggleClass('rotated');
        $('#menutoggle').toggleClass("hov");
    });

    $('a#hide, a#hide2').click(function(){
        $('#mobilemenu').hide();
    })


});

Answer №2

insert

$('#menutoggle').trigger('click');

into

$("#mobilemenu li").click(function() { ...

Answer №3

If you simply include this snippet in your script, everything will function smoothly:

$("li").click(function () {
   $("#menutoggle").css( "background-color", "blue" );
});

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

Gulp: Adding to Dest without Overwriting

I have the following code snippet: gulp.task('concat-uglify-js', function() { return gulp.src(src + 'js/*.js') .pipe(concat('angular-filemanager.min.js')) .pipe(uglify()) .pipe(gulp.dest(dst)) }); gulp.task(&ap ...

Implementing the <bgsound> element in HTML

Can you explain the purpose of the <bgsound> element in HTML? ...

Engaging with a JSON data structure

I have experience using ajax for writing to sessions and performing inserts, but now I need to receive a response. While I've managed to get it working somewhat by going through examples, I'm currently stuck at a certain point. I am in search of ...

Functionality of the user profile in MEANJS

I am currently working on developing a basic app using meanjs. I have implemented two modules: the standard user module and a posts module. My goal is to create a user profile page that will showcase specific user information and list the posts associated ...

"Resolving an inconsistency problem with the Vary header in Htaccess

Could someone help me identify the issues in my htaccess code? I'm encountering errors: This response is negotiated, but doesn't have an appropriate Vary header. The resource doesn't send Vary consistently. ## add vary header <IfModule ...

Angular JS Route Hijacking

Currently, I am developing an AngularJS application with Firebase as the backend system. One of my main goals is to require users to log in before accessing any part of the application. To achieve this, I plan on using Firebase's User Authentication ...

Leveraging the power of AngularJS and Bootstrap, create a vertical nested tab interface that dynamically

After successfully creating a Dynamic Tab using AngularJS with Bootstrap, I decided to add Sub Tabs under one of the tabs dynamically. While this feature is working fine, I encountered an issue when trying to move to another main tab and then returning to ...

Issues with modifying Twitter Bootstrap are not being resolved

Attempting to customize the appearance of the btn-primary element in Bootstrap by utilizing a local file located at /public/styles.css. Experimented with both Jade and Stylus, but unable to successfully alter the background-color of the btn-primary. Any ...

Troubleshooting tips for resolving encoding issues during XML to JSON conversion

I am currently working with an XML file and I need to convert it into JSON format using Node.js and then display it in HTML. Below is the content of the XML file: <?xml version="1.0" encoding="windows-1251" ?> <ROWDATA> &l ...

Changing an HTML file formatted with Jquery into a Phonegap compatible version

I am working on converting my HTML file into an Android app using phonegap. However, I have encountered an issue where the buttons for showing/hiding images using JQuery have stopped functioning properly. Despite linking both the normal jQuery file and th ...

Using an array as a route parameter triggers a "Expected to not repeat" warning

Every time I attempt to set up a router-link with an array as the parameter, the link functions properly but triggers the following warning : "Missing param for named route "start-run": Expected "files" to not repeat, but received ["aaa"] router.js .. ...

Fixed Navigation - Employing stickUp.js extension

* { box-sizing: border-box; } body { margin: 0; padding: 0; font-weight: 500; font-family: 'Helvetica Neue', sans-serif; } main {} .bckgrnd { background-position: center center; background-repeat: no-repeat; background-attachment: ...

What is the best way to adjust the camera position in three.js while keeping it from rotating?

I've been working on an interface using three.js and the CSS3DObject rendering tool. To control movement, I've disabled rotation by setting the orbit to 0, allowing only panning and zooming. Just a heads up - I'm also utilizing Orbit Contro ...

The dynamic sidebar menu in Adminlte 3 with bootstrap-4 loaded from ajax is not functioning properly, presenting issues with sidebar

Is there a way to fetch dynamic sidebar menu data from the database using AJAX in the adminlte 3 dashboard along with bootstrap 4? I have tried loading the sidebar menu data dynamically using AJAX, but the sidebar open/close functionality is not working pr ...

The ng-repeat function in AngularJs does not display the data despite receiving a successful 200 response

As part of my academic assignment, I am exploring Angularjs for the first time to display data on a webpage. Despite receiving a successful http response code 200 in the Chrome console indicating that the data is retrieved, I am facing issues with displayi ...

How to bypass jQuery validator for a specific field in a form

I am struggling to find a way to skip filling in certain form fields when a specific button is clicked. For example: In my scenario, I need to bypass the newpassword and newpassword2 fields (which are required for changing passwords) because I have provi ...

Step-by-step guide on crafting a scrolling marquee with CSS or Javascript

I am in need of creating two marquees for a website project. The first marquee should showcase a repeating image while the second one should display links, both spanning the browser window at any size. It is crucial that the marquee items are displayed fro ...

Adjust the position of elements based on their individual size and current position

I am faced with a challenge regarding an element inside a DIV. Here is the current setup... <div id="parent"> <div id="child"></div> </div> Typically, in order to center the child within the parent while dynamically changing i ...

What is the best strategy to prevent reflow and flickering when loading images on a website?

My website displays an image on the left with text on the right, but when the server is busy or the connection is slow, the page flickers. It initially shows the text on the left and then suddenly moves it to the right once the image loads. This issue see ...

Retrieve the content from a textarea and insert it into a different textarea with additional text included

Users can input HTML codes into a textarea named txtar1. A 'generate' button is available; Upon clicking the 'generate' button, the content of txtar1 will be transfered to another textarea named txtar2 with additional CSS code. Here&ap ...