Jquery Toggle not applying class change on mobile devices

Check out my website: . There's a menu option called Services with a dropdown feature. Here is the code for it:

<li class="dropdown menu-services"><a class="dropdown-toggle" href="#" data-toggle="dropdown" data-target="#">Services <b class="caret"></b></a>
<ul class="dropdown-menu">
    <li class="menu-online"><a href="http://annapiotrowski.com/online-health-coaching/">Online</a></li>
    <li class="dropdown menu-in-person"><a href="#">In Person</a>
<ul class="dropdown-menu">
        <li class="menu-classes"><a href="http://annapiotrowski.com/classes/">Classes</a></li>
        <li class="menu-corporate-wellness"><a href="http://annapiotrowski.com/corporate-wellness/">Corporate Wellness</a></li>
    </ul>
</li>
</ul>
</li>

If you're on mobile, clicking on Services should expand the menu to display the items in the dropdown. I'm using this jQuery script to toggle it:

<script>

(function($) {

    $(document).ready(function(){

        var newWindowWidth = $(window).width();
        if (newWindowWidth < 768) {

        $(".dropdown").on("click", function() {

        $(".dropdown").toggleClass("open");


        });

        }

    });

})( jQuery );

</script>

When the page is at the top, I can click on the Services menu item and see the dropdown items.

But when I scroll down and try to do the same mid-page, it doesn't work at all :( Clicking on Services does nothing. What could be the issue?

I've attempted this fix:

(function($) {

    $(document).ready(function(){

        var newWindowWidth = $(window).width();
        if (newWindowWidth < 768) {

        $(".dropdown").on("click", function() {

        $(".dropdown").toggleClass("open");


        });

        $(".dropdown").off("click").on("click", function() { $(".dropdown").toggleClass("open"); });

        }

    });

})( jQuery );

Still no luck.

Answer №1

In order to properly identify the target of a click event, it is recommended to use the "context" keyword within the click handler function. I have optimized your click handler code below and tested it successfully on your website:

 $(".dropdown").off("click").on("click", function() {
    $(this).toggleClass("open"); 
    $(this).find(".dropdown").toggleClass("open");
 });

Answer №2

In the event that you are using a mobile device, it seems like the issue arises from having two menu elements in the document.

If you find yourself at the top of the page, try clicking on the Services menu item (it should function properly) and keeping it open as you scroll down. This way, you will be able to view the second menu element as you continue scrolling.

You just need to make some adjustments to your JavaScript code to accommodate the second menu.

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

Updating images based on dropdown selection using Bootstrap

Can you provide guidance on changing the img src (<img src="img/website_building.jpg"...) within a Bootstrap dropdown list based on item selection? Is this functionality achievable? <tr> <td class="bold text-primary"> ...

Changing the color of a div's text based on its content with CSS

I am facing a challenge in styling the text inside a “div” element using a Javascript function. Initially, when the page loads, these “divs” contain no value. The values of the "divs" will change between "Open" and "Closed" twice a day when the Jav ...

What is the best way to position an image in the center over a video using HTML and CSS?

I am currently developing a website with a background video and I want to overlay a company logo on top of it while keeping it centered. My coding language of choice for this project is using HTML and CSS. Here's the HTML code snippet I have for this ...

Error: The function $(...).maxlength is not recognized - error in the maxlength plugin counter

I have been attempting to implement the JQuery maxlength() function in a <textarea>, but I keep encountering an error in the firefox console. This is the code snippet: <script type="text/JavaScript"> $(function () { // some jquery ...

The jQuery function, toggle(), allows for easy toggling

One challenging aspect of my website is handling dynamic divs and switching between two different divs within them. I'm struggling to find a way to do this while still maintaining the original value of the div. I've tried writing some code, but ...

Is there a way to incorporate text at the end of a row in HTML?

I have a photo and some text on my website. The photo is displayed on the left side, while the text appears nicely on the right side. Now, I am looking to include an additional block of text below the existing text. How can I accomplish this? What steps ...

Why does this inner HTML table always adjust its width based on the content within it? Is there a way to make it match the width of its container instead

I'm not very familiar with HTML and CSS, and I've come across a problem. Here is the CSS structure in question: <!-- TECHNICAL REFERENCE: --> <div id="referenteTecnicoTab"> <table width="800px" class="standard-table-cls table-he ...

Tips for ensuring all images are the same size within a div element

https://i.stack.imgur.com/EkmWq.jpg Is there a way to make sure all the images fit perfectly inside their respective border boxes without appearing stretched? I've tried setting fixed height and width within a div, but they always end up looking off. ...

Transform the look of an inactive hyperlink

Can the visual style of an HTML link be modified when it is disabled? For instance, by implementing something like this: a.disabled { color:#050; } <a class="disabled" disabled="disabled" href="#">Testing</a> The code snippet above appears ...

jQuery regex failing to display latest changes

I am running into some issues with my custom jQuery snippet that is supposed to dynamically display the results. Each H2 tag contains an image, and I want to ensure the image is positioned correctly next to the word "test." Here is the script in question: ...

Only in Firefox, there seems to be an issue with how the CSS

Using JavaScript, I have added two buttons to the left of the filter box: This is the HTML code for the buttons: Here is the CSS style for the buttonfile: .buttonfile { text-indent: 0; float: left; display: block; margin-right: 16px; margin-top ...

Creating an image rollover effect when hovering between two images

I am currently working with React and trying to change 2 images by hovering over them. Here is what I have accomplished so far: Card.js <div className="image-wrapper"> <img src={sprites.front_default} className="image" a ...

Which is better for cycling through a list of items: CSS or JavaScript?

Currently, I have a navigation bar set up as an unordered list (<ul>), but some list items are not visible. I want to implement functionality where clicking arrows will move the elements left or right by hiding or showing the leftmost or rightmost it ...

Troubleshooting MVC3 @Ajax.ActionLink: Unexpected Page Display Instead of Target UpdateId Replacement

My current project is built with VS2010/.net4/MVC3 (MVC4 encounters the same issue) I am attempting to invoke an AJAX function in my project @Ajax.ActionLink("Display ALL Cbms results", "All" , "Cbms", new AjaxOptions() { HttpMethod = "GET", Upda ...

Identifying when an element is in or out of view using Next.js and TypeScript

I'm currently working on an app using Next and Typescript. The app features a navigation bar at the top of the screen, and I need it to change its style once it reaches a certain point in the view. I attempted to use jQuery for this purpose, but encou ...

What steps can be taken to activate the remember password feature when using an AJAX login system?

I've developed a web application with a basic login box, utilizing jQuery AJAX for the login process. However, I'm facing an issue with prompting the browser to remember the password. How can I trigger the "Do you want the browser to remember th ...

Implementing a tooltip popup inside the header cell of the ordering table

Apologies in advance for my lack of experience with coding more complex website features. I'm attempting to incorporate a tooltip popup into one of the header cells to provide additional information to users. While I have all the necessary component ...

Troubleshooting issue with alignment in Material UI using Typescript

<Grid item xs={12} align="center"> is causing an issue for me No overload matches this call. Overload 1 of 2, '(props: { component: ElementType<any>; } & Partial<Record<Breakpoint, boolean | GridSize>> & { ...

Is there a way to ensure that all elements on a page display properly across all screen resolutions without being cut off?

My website consists of three main elements: pictures, Cards (containing pictures and various typography), and Buttons. These elements are arranged in a column layout. The issue I am facing is that the pictures and buttons get cut off on the screen due to ...

Using jQuery and Ajax to fade in content after all images and other assets have finished loading

I've encountered an issue with loading pages via ajax for users with custom URLs. For example, a profile is usually found at http://example.com/users/Dan, but if a user has a custom URL like http://example.com/DansCustomURL, I need to fetch their desi ...