Issue with Jquery's .addClass and .removeClass functions

I'm having trouble implementing a dynamic menu on my webpage using jQuery. I've tried writing the code myself but it doesn't seem to be working as expected.

The structure involves three classes: .dead, which sets display: none; in CSS, .header-main, representing one div, and .header-main-menu, which should appear when a button is clicked.

$(document).ready(function () {
    var trigger = $('a.header-main'),
    var mainstuff = $('.header-main'),
    var menustuff = $('.header-main-menu');

    function buttonSwitch() {
        mainstuff.addClass('.dead');
        menustuff.removeClass('.dead');
        trigger.click(function () {
            buttonSwitch();
        });
    }
});

Despite my efforts, the button doesn't trigger any action. As I am new to jQuery, I would greatly appreciate any assistance you can offer. Thank you for your help! :)

Answer №1

When using the addClass() or removeClass() (or toggleClass()) methods, you should not include a dot before the class name.

mainstuff.addClass('dead');
menustuff.removeClass('dead');

Additionally, when selecting elements, decide whether you want to end every line or chain selections together. If chaining, do not use var again.

For example:

var trigger = $('a.header-main'),
mainstuff = $('.header-main'),
menustuff = $('.header-main-menu');

Or separately:

var trigger = $('a.header-main');
var mainstuff = $('.header-main');
var menustuff = $('.header-main-menu');

If your trigger selector does not match anything in your HTML, make sure to fix it using something like $('.main-button > a'). And consider using Codepen or a similar platform if you want to showcase HTML, CSS, and JS code simultaneously.

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

What is the best way to incorporate a custom modal created with HTML/CSS into my Bootstrap website?

I am facing an issue with the modal implementation on my bootstrap website. The modal is supposed to open when a user clicks a button. Strangely, it was working perfectly fine on another HTML/CSS file, but once I added it to this Bootstrap file, it stopped ...

Restart simplemodal

In relation to this jQuery plugin found at , I am facing a situation with my ajax function where it takes some time to retrieve the data - therefore, I would like to display a spinner in the modal div. $("#basic-modal-content").modal(); $("#basic-modal- ...

Arrangement of SVGs within one another

I am working on achieving a layout with multiple SVG elements arranged in a specific way. This layout involves a top-level gray box containing several orange boxes, each of which holds red boxes. The width and height of the top-level SVG are known. https: ...

When running the PHP script, the output is shown in the console rather than in the

Here is a PHP script snippet that I am working with: <?php add_action('wp_ajax_nopriv_getuser', 'getuser'); add_action('wp_ajax_getuser', 'getuser'); function getuser($str) { global $wpdb; if(!wp_verif ...

Values do not appear as expected post PDF conversion using wkhtmltopdf

Currently, I am updating certain values within my HTML page by following this process: The function: function modifyContent(){ var newTitle = document.getElementById('myTextField1').value; if( newTitle.length==0 ){ alert('Plea ...

Incorporating partial page loading with PHP and ID links

Having some trouble with partial page loads and PHP variables. Currently, I have it set up to work, but when trying to include a PHP variable, it seems to be causing issues. The page successfully loads without the variable: <li><a href="javascri ...

Retrieve the image information from a specified element in the Document Object Model

Is it possible to extract the image data of a DOM element using standard JavaScript or browser extensions? I'm considering scenarios where: Creating an offscreen DOM element Populating it with dynamically styled CSS content Retrieving its image dat ...

What is the method for generating an oval shape with a border-radius in CSS?

Is there a way to create an oval shape instead of a rectangle with rounded edges using CSS? Below is the code snippet I have been working with: div { position: fixed; border-radius: 25px; background: rgba(0,0,0,.5) width: 100px; height: 50px; ...

Click on the div to add items from an array into it

I have a unique set of lines stored in an array: var linesArr = ["abc", "def", "ghi"]; In my JavaScript, I dynamically create and style a div element using CSS: var div = document.createElement("div"); div.className = "storyArea"; div.in ...

Is there a CSS rule that can alter the appearance of links once they have been clicked on a different webpage?

Just starting out here. Imagine I have 4 distinct links leading to the same webpage - is there a way to modify the properties of the destination page depending on which link was clicked? For instance, clicking on link1 changes the background color of the d ...

I'm attempting to retrieve a JSON response with Jquery-Ajax, but I'm running into issues. The selection dropdown is loading without any options

Upon hitting http://localhost:8082/getCountries, the SOAPUI (GET) response is as follows: HTTP/1.1 200 Content-Type=application/json Transfer-Encoding=chunked Date=Mon, 18 May 2020 03:38:46 GMT Keep-Alive=timeout=60 Connection=keep-alive [{"countryI ...

Implementing script loading within the Angular scope

I'm attempting to load a custom script from the database based on client-side logic. I am having trouble figuring out how to make it function properly. Here is my controller code: 'use strict'; angular.module('youshareApp') . ...

jQuery mistakenly sends a GET request instead of a POST request

Has anyone encountered an issue where attempting to send a POST request to Flask using jQuery results in a GET request instead? This is a new problem for me, and I'm unsure why it's happening. jQuery system_data = { "system_name":"system_name ...

What is the best way to execute a function based on its name, which is provided as a parameter

Is there a better way to call a function by its name, passed as a parameter in JavaScript? Here's my scenario: I have a switch case where I need to set the data for an API ajax call. Once the ajax call is complete, I want to call a specific print func ...

The combination of Bootstrap and Django does not support responsive design

My code is not working correctly, and I'm struggling to identify the errors. The website I'm working on has subpages, but the responsive design doesn't seem to be functioning properly. Could this issue be related to mistakes in my HTML/CSS c ...

The MUI Modal is too large for the display

I'm using a Modal component from MUI in my project and I am facing an issue with displaying a large JSON object inside the modal. The problem is, the top of the modal extends beyond the screen height and even after making it scrollable, I am unable t ...

Is there a way to trigger the activation of the datepicker during the `onLoad` event?

For my project, I am utilizing this datepicker. While I am familiar with using scripts to handle changes in the date value, I am unsure of how to implement it on page load. $('.date_set .date').datepicker({ startView : 0, ...

JS seems to kick in only after a couple of page refreshes

I'm facing an issue with my 3 columns that should have equal heights. I've implemented some JavaScript to achieve this, which you can see in action through the DEMO link below. <script> $(document).foundation(); </script> <scri ...

Content from PHP's unlink() function continues to persistently display

I run an Angular front end along with a PHP back end for my website. I utilize PHP's unlink function to remove specific images from Angular's assets folder: $fileToDelete = "../src/assets/images/".$name; unlink($fileToDelete) or die("Error delet ...

I am attempting to execute an ASP.NET function using an HTML submit button, but for some reason the function is not being triggered

I need the function submit_click to be triggered when the submit button on the HTML section is clicked <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %> <html xmlns="http://www.w3.o ...