What could be causing my dropdown menu code to malfunction?

Having trouble with the drop-down menu! I used "display: none;" in CSS to hide the list, but I'm not sure if it's the best way. This idea was borrowed from a Codecademy project.

I know there may be some cringe-worthy code here, but please bear with me, I am a complete beginner at programming! Thank you!

$(document).ready(function() {
  $('p').hover(function() {
    $(this).find('ul').fadeToggle(400);
  });
});
* {
  margin: 0;
  padding: 0;
}
.main {
  margin: 0 auto;
  height: 400px;
  width: 400px;
}
.container {
  max-width: 230px;
  max-height: 300px;
  margin: 0 auto;
  background-color: #024F79;
}
p {
  text-align: center;
  color: #fff;
  border-bottom: 1px solid #fff;
  font-size: 18px;
  padding: 8px;
}
ul {
  list-style-type: none;
  display: none;
}
ul li {
  padding: 10px;
  border-bottom: 1px solid white;
  color: white;
  font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>


<body>
  <div class="main">
    <div class="container">
      <p>How do I ?</p>
      <ul>
        <li>View my Transcript</li>
        <li>View my Conformation Page</li>
        <li>Register for Courses</li>
        <li>Pay for Courses/Exams</li>
      </ul>
    </div>
  </div>

</body>

Answer №1

When using $(this).find('ul'), the search is conducted within the <code>p
element due to the context being set as this. An alternative method would be to utilize .next().

$(this).next('ul').fadeToggle(400);

Alternatively, a more efficient approach involves restructuring the HTML by enclosing both the p and ul elements within a div that has an id for easier UI fading.

<div id="menu">
 <p>How do I ?</p>
 <ul>
  <li>View my Transcript</li>
  <li>View my Conformation Page</li>
  <li>Register for Courses</li>
  <li>Pay for Courses/Exams</li>
 </ul>
</div>

Subsequently, utilize the original code but direct it towards the #menu item.

$('#menu').hover(function() {
 $(this).find('ul').fadeToggle(400);
});

Explore the jsFiddle Demo

Answer №2

If you're looking to avoid JavaScript, consider utilizing CSS-only solutions, unless animation is a requirement.

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

Unable to view Google Map markers within my XPath elements while using Selenium?

Looking to extract data from a Google Maps applet. The specific page can be found here: You can click on items on the map to view displayed information. While typical Google Maps show marker elements, I cannot see them on the provided link when inspecti ...

Every time I try to install create-react-app, I keep encountering a frustrating 'network Socket timeout' error

$ npx create-react-app amazon-clone npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. Creating a new React app in D:\js\faceboom. npm WARN config global `--global`, `--local` are deprecated. ...

Persist in the face of a mishap in javascript

Two scripts are present on the page. If the first script encounters an error, the second script will not function properly as a result. Is there a way to make the second script ignore any errors from the first one and still work? Please note that I am str ...

Having trouble compiling Vue.js for Internet Explorer 11

After seeking help on Stack Overflow and conducting extensive research, I have finally configured my babel.rc file as follows: { "presets": [["env", { "modules": false, "uglify": true, "targets": { "browsers": ["> 1%", "last 2 versi ...

Key factors to keep in mind when comparing JavaScript dates: months

Check the dates and determine if the enddate refers to the following month by returning a boolean value. Example startdate = January 15, 2020 enddate = February 02, 2020 Output : enddate is a future month startdate = January 15, 2020 enddate = January 2 ...

Server connectivity issues due to air disruptions

Our team has developed an innovative Air application for delivering course material to our clients. However, we have encountered a connectivity issue with some clients that we were able to replicate. To provide some context, the application utilizes jQuer ...

How can I create my own unique scrolling behavior in JavaScript?

Looking to create a similar effect as seen on this website: where the vertical scrollbar determines the movement of the browser "viewport" along a set path. I believe that using Javascript to track the scroll bar value and adjust background elements acco ...

How can I extract information from an HTML table using AngleSharp?

Seeking a way to extract song data from a playlist on a music streaming website This table contains song information: <tr class="song-row " data-id="ef713e30-ea6c-377d-a1a6-bc55ef61169c" data-song-type="7" data-subscription-links="true" data-index="0" ...

Tips for getting the DIV:hover effect to work in Internet Explorer 8

Can someone provide me with the steps to get DIV:Hover functioning in Internet Explorer 8? ...

What are your thoughts on nesting an "li" tag within an "a" tag?

Consider this scenario: <ul> <a href="http://google.com"><li id="someId"></li></a> ... ... ... </ul> This is a solution I came up with to work around my current css styling. Now, I'm debating whether or not to ref ...

JavaScript - Clear the localStorage when closing the final tab of a website

Currently, I am working with AngularJS and utilizing $window.localStorage to store the username of a logged-in user. Since localStorage needs to be explicitly deleted, I have implemented an event listener for $(window).on('unload', function(){}) ...

Get a collection of images packed into a single zip file

My current function downloads multiple images and saves them to a user's "download" folder, although it only works in Chrome. I am looking to enhance this function by combining the downloaded images into a single zip file. Below is my existing code. ...

What is the best way to assign an onClick event in React while using document.createElement()?

I am using document.createElement to create an <input>. How can I assign the onClick property in React? var input = document.createElement("input"); input.onClick = {setCount()}; //??? Here is the React code snippet: <input type="s ...

Why do variables maintain the same value across modules when using the require function in Node.js?

We have a scenario where we have multiple files in the same directory: data.js var anArray = [1,2]; module.exports = anArray; mod1.js var data = require('./data'); module.exports = data; mod2.js var data = require('./data'); modul ...

What is the reason behind the warning "Function components cannot be given refs" when using a custom input component?

When attempting to customize the input component using MUI's InputUnstyled component (or any other unstyled component like SwitchUnstyled, SelectUnstyled, etc.), a warning is triggered Warning: Function components cannot be given refs. Attempts to acc ...

Incorporate a button into an input element

I have a created a custom login form and I'm trying to add a "FORGOT?" text/button next to the password field. If you want to see an example, visit this website and click on the login button: <script src="https://stackpath.bootstrapcdn.com/boot ...

Attempting to streamline this particular JavaScript function

I have been struggling with a function that I believe could be written more effectively. My goal is to simplify it while still maintaining its functionality. function changeLetters(text) { text = text.toLowerCase(); for (var i = 0; i < text.length; ...

Sets a minimum width for a Bootstrap panel

I have a panel panel-default on my page to display some data. I want the panel's min-width to be 720px, and if the window is resized to less than 720px, I want a horizontal scrollbar to appear. However, the panel cannot shrink below 720px similar to u ...

Effective strategies for minimizing the bundle size of your NextJs application

Recently, I launched my first NextJS app and was surprised to see that the initial bundle size is around 1.5Mb, which seems quite large for me as a beginner in using Nextjs. I have shared an image of the yarn build and also my package.json. All the pages ...

Unit testing React Native for inputting text values

I am attempting to append a unit to the value of a textInput: https://i.sstatic.net/EqswS.jpg I have attempted to achieve this with the following code but have not been successful: value = { this.state.totalWeight + " Kgs"} I also tried adding ...