What could be causing the malfunction of slideToggle in this situation?

I have been troubleshooting this issue all night and I am completely stuck. Can anyone figure out why the navigation menu is not opening?

Here is the HTML code:

<ul id="nav">
    <li><a href="item1.html">item1</a></li>
    <li><a href="item2.html">item2</a></li>
</ul>
<div id="menu">
    <div id="menu-button"><span id="menu-text">Menu</span> <img src="image/nav.png"></div>
</div>

And here is the CSS (please note that I am currently working on a responsive design):

#menu {
    position: relative;
    display: block;
    width: 100%;
    height: 50px;
    background-color: #1F5865;
}

#menu-button {
    position: relative;
    width: 100%;
}

#menu-text {
    position: absolute;
    top: 13px;
    left: 15px;
    font-size: 18px;
    font-weight: bold;
    padding-right: 10px;
}

#menu-button img {
    position: absolute;
    right: 15px;
    top: 10px;
}

#nav {
    display: none;
    position: relative;
    width: 100%;
    list-style-type: none;
    padding: 0px;
    margin: 0px;
    font-family: Raleway, Arial, sans-serif;
}

#nav li {
    list-style-type: none;
    text-align: center;
    background-color: #297585;
    padding: 0px;
    width: 100%;
}

#nav a {
    display: block;
    padding: 14px 0px; 
    border-bottom: 1px solid #1F5865;
    color: #FFF;
    width: 100%;
    text-decoration: none;
}

Lastly, here is the JavaScript code (included within the <head> tags):

<script src="js/jquery-1.10.2.min.js"></script>
<script>
$("#menu").click(function(){
    $("#nav").slideToggle();
});
</script>

Answer №1

1) Utilize the # symbol to reference an ID
2) Ensure that you wait for the objects to become available

Check out the Live Demo demonstrating the use of HEAD instead of onload

<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(function() { // Executes when the document is ready    
  $("#menu").on("click",function(){
    $("#nav").slideToggle();
  });
});
</script>

Answer №2

In jQuery, the id selector should always start with a #. For more information on jQuery selectors, you can visit this link.

$(document).on('click','#menu',function(){
    $("#nav").slideToggle();
});

Check out the live demonstration here.

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

Is the Foundation Orbit image slider experiencing technical difficulties?

Hey there, I've been experimenting with Foundations Orbit media container to create an autoplay image slider using HTML and CSS. While I'm more comfortable with HTML/CSS than JavaScript, I have struggled to find a simple and cost-effective soluti ...

unable to insert logo into HTML

I am customizing an HTML template, but the dimensions of the logo in the template are smaller than my logo. Despite adding my logo to the template, it is not showing up. @media (max-width: 673px) { #logo { margin-left: 20px; } } #header #logo ...

Issue with Angular filter not being effective within ng-repeat

For all code regarding this issue, please visit: Filter: <input type="text" ng-model="search"> <tr ng-repeat="(id, group) in groups | filter:search" class="editing-{{group.editing}}"> The goal is to have the rows filtered out based on the in ...

Angular JS - Sorting with ng-repeat

I'm completely new to AngularJS and facing a filtering issue with a custom function. My goal is to have: Checkboxes for product categories Checkboxes for product sub-categories A showcase of products When a user clicks on a category, the sub-catego ...

Curious about learning the Xpath?

This is the HTML content <dd id="_offers2" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer" class="wholesale nowrap "> <span itemprop="price" class="Hover Hover Hover">$46.29</span> / each <meta itempr ...

Turn off automatic downloading of embedded frame content in Safari for iOS

I am encountering an issue with a modal that displays a PDF and offers two options - one to print and one to download. The download option uses a blob with content-type: application/octet-stream, while the print option utilizes a blob with content-type: a ...

Display the current count of selected radio buttons in real-time

I am working with radio buttons that are generated dynamically using a 2D array within a while loop. I want to display the number of radio buttons checked when one is clicked. Here are my radio buttons: $n=0; while($row=mysqli_fetch_row($rs)){?> <f ...

Center text within a dropdown using Bootstrap styling

In a row within my inline form, I have both an email input and a dropdown. However, after resizing the page, the text within the dropdown is no longer positioned correctly (text on the left, arrow on the right). I attempted to fix this by using: text-alig ...

Designing CSS outlines to create shapes

I want to design a unique navigation for my website without using images, so I thought using shapes would be a cool idea. Specifically, I am trying to create a crescent moon shape with a border. My goal is to have a border around the red part of the cres ...

Using String Class Constraint in TypeScript

I am attempting to create a class that includes a string constraint, but I keep encountering an error at the get scale() function. class Scaling<T extends string> { _scale = ""; constructor(props: T) { this._scale = props; } ...

Triangle Top Megamenu Design

Currently, I am implementing a navbar in bootstrap v4.4.1 that includes a mega menu dropdown feature. In my design, I want to include a triangle at the top of the dropdown menu. While I have managed to add the triangle successfully, I am facing difficulti ...

Accessing a variable from one function within another function in Vue

How can I access and utilize the ctx variable from the initGrid() function in the drawGrid() function? Despite trying to use "this," it seems that the variable cannot be accessed within the drawGrid() function. <script> export default { data: ( ...

Material-inspired Design Device Compatible DIV slide with JS, JQuery, and CSS

My goal is to achieve something similar to this: Desired Live Website I am looking for a feature where clicking on the div will slide in content from the right, load an external page inside it, and close when prompted. The slider div should be device c ...

Using jQuery for Dragging and Dropping Elements within Dynamically Loaded Divs with

Is it possible to drag an element on the page into a droppable element inside an ajax loaded div? The code works fine when the droppable element is placed directly in the regular page, but not within the ajax loaded div. It seems like the issue may be rela ...

What is the best approach to eliminate an element from the array cart using JavaScript?

I currently have an array: [ 0: {title: "Banana", price: 1.00, count: 2}, 1: {title: "Taco", price: 3.99, count: 1}, 2: {title: "Burrito", price: 6.50, count: 1}, 3: {title: "Soda", price: 1.25, count: 1} ...

Images that have been resized on Android appear blurry when viewed on the second page

Customizing the Toolbar: #main_bar { background: url(../images/logo.jpg) 99% 50% no-repeat; background-size: auto 80%; } The original size of logo.jpg is 220x266px The toolbar has a fixed height of 46px Therefore, the background image appears t ...

I'm experiencing difficulties loading data using AJAX

I'm facing an issue with an old script that used to load IP from an xml file. Everything was running smoothly until about six months ago when I tried to use it again and encountered some problems. I'm not sure what went wrong. Could there have be ...

Assign the "this" keyword of App.vue to the Vuex state

I have discovered a workaround for accessing vue-router and other services that only works within a Vue component. By saving the "this" of the Vue app in the state of Vuex within the created option of App.vue. Is this a good approach or could it potential ...

The option list in AngularJS is cleared when an option is selected

In my current project, I am developing a django-tastypie api application with angularjs as the JavaScript framework. The main part of this application involves managing curriculum objects, each containing a list of grade objects and each grade object furth ...

Retrieve the specific value of an item saved in localStorage

How can I extract the value from localStorage without retrieving the key name? localStorage.setItem('token', tokenJson); console.log(JSON.parse(localStorage.getItem('token'))); When I use console.log, it displays something like {"toke ...