Conceal and reveal submenu options

I have been exploring a jQuery function that toggles between hiding and showing different divs when a menu item is clicked.

However, I am facing an issue where clicking on the same menu item does not close the newly opened div as intended. Additionally, I would like to implement a hide function for the 'submit' div class when clicked.

Any tips or suggestions would be greatly appreciated.

https://jsfiddle.net/a9ykpdwz/

Jquery

$("#one, #two, #three").hide();

$(".one, .two, .three").click(function (e) {
e.preventDefault();
$("#" + $(this).attr("class")).fadeIn(1000).siblings('#one, #two, #three').fadeOut(1000);
});

HTML

<div id="nav">

<a href="#" class="one">WORK</a>
<a href="#" class="two">ABOUT</a>
<a href="#" class="three">CONTACT</a>
</div>

<div id="one">
<ul>
<li>xx</li> 
<li>Spaces</li>
<li>Form</li>
<li>Mind and Body</li> 
<li>Moving Image</li> 
<li>White Trails</li> 
<li>Foreign Views</li>
<ul>
<div class="submit"> close </div>
</div>

<div id="two">Text</div>
<div id="three">Text</div>

Answer №1

To hide all three divs on submission, you should use fadeToggle() instead of fadeIn(). The code snippet below achieves this functionality:

$("#one, #two, #three").hide();

$(".one, .two, .three").click(function(e) {
  e.preventDefault();

  $("#" + this.className).fadeToggle(1000).siblings('#one, #two, #three').fadeOut(1000);
});

$('.submit').click(function(e) {
  $('#one, #two, #three').fadeOut(1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="nav">

  <a href="#" class="one">WORK</a>
  <a href="#" class="two">ABOUT</a>
  <a href="#" class="three">CONTACT</a>
</div>

<div id="one">
  <ul>
    <li>xx</li>
    <li>Spaces</li>
    <li>Form</li>
    <li>Mind and Body</li>
    <li>Moving Image</li>
    <li>White Trails</li>
    <li>Foreign Views</li>
  </ul>    <!-- corrected a missing / here -->
  <div class="submit">close</div>
</div>

<div id="two">Text</div>
<div id="three">Text</div>

Answer №2

  1. When creating the event for the .close element, make sure it is set up correctly.
  2. To display a new element while hiding the previous one, set the display to none without transitions if the elements are stacked. Otherwise, consider changing the elements to absolutes.

Check out this fiddle for an example: https://jsfiddle.net/a9ykpdwz/2/

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

Revolutionizing Interaction: Unveiling the Power of Bootstrap 3

Would you be able to assist me in customizing buttons to resemble a typical dropdown link? <div class="dropdown"> <button class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">Dropdown <span class="caret"&g ...

Ensuring Jquery validation is triggered before submitting a form to PHP

I have been struggling with a particular issue and decided to seek help. I have a form filled with user data that I have successfully validated using js/jQuery before sending it to php for processing. Here is how I am achieving this: form.submit(functio ...

Encountering challenges with CORS implementation in a test application

I am struggling with an issue while trying to send a request to a website for ping. I have tried using jsonp, but it's not working as expected. I attempted to implement cors, but I keep getting the error message "No 'Access-Control-Allow-Origin&a ...

Sending numerous parameters in a form

I'm having an issue with my code where the 'name' variable is POSTed but the 'project' variable is not when submitting through an 'onchange' event. I need both variables to go to session variables on submit. Can anyone sp ...

Is there a way to replicate the functionality of Python's zip() function in JavaScript

Struggling to translate this function into JavaScript... def pascal(n): row = [1] k = [0] for x in range(max(n,0)): print(row) row=[l+r for l,r in zip(row+k,k+row)] return n>=1 I'm encountering difficulties with th ...

Issue with background image repeating incorrectly when resizing

I've recently added an image as the background for my website using the following style: #whole_wrapper{ background-image: url(../images/wrapper_bg.jpg); background-repeat: repeat-x; width: 100%; float: left; height: 116px; } ...

Adding data to an array using jQuery

I have a list of items called data and an empty array called quotations: var data = {}; var quotations = []; My goal is to populate the quotations array with data values. Each time I add new data, it is successfully added, but all data values end u ...

Deciphering JSON using JavaScript

Looking to decode a URL using Javascript? let url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=London&destinations=drove&mode=driving&language=en&sensor=false"; fetch(url) .then(response => response.json()) .th ...

Unusual shadow effects with three.js

I'm still new to three.js and webgl, and I'm encountering some odd-looking shadows when using a directional light. Could somebody help me out? Below is the code I have for the renderer: this.renderer.shadowMapEnabled = true; this.renderer.shad ...

Exploring ways to utilize removeEventListener in React Native

Can someone assist me with converting this code to React? I am new to using React and struggling with the implementation. Thank you in advance! <progress id="bar" value="0" max="110"></progress> <button onClick={i ...

Maintain an array with a maximum of 5 elements in MongoDB by removing the oldest element when a new one is inserted

I have a unique requirement where I need to maintain an array of objects in MongoDB with a maximum of 5 elements. Whenever a new element is added, the oldest one should be removed to ensure that there are always only 5 elements in the array. Is there a way ...

React: Update the hidden input's value to reflect the total number of spans clicked

I have a component called Rating that displays a star rating like this: var Rating = React.createClass({ render: function () { return ( <div className="rate-line"> <div className="rate-title">{this.props ...

Updating the state of an object nested within another object in React JS

So, I have a nested object within another object and I need to update the state of one or more properties in the inner object. This is my current state: const [products, setProducts] = useState({ name: '', type: '', price: '& ...

Display Mailchimp subscription form adjacent to button click on a WordPress website

Seeking assistance with integrating a MailChimp simple subscription form (requires email and submit) next to a button on my Wordpress page. The desired functionality is a straightforward button labeled "Newsletter." When clicked, a small form should conve ...

Error with the setInterval() method, function is not executing

UPDATED CODE SNIPPET: <script> $.ajaxSetup({ cache : false }); function fetchMessage() { $.get("php/getMessage.php?q=1" + "&" + Date.now(), function(data) { $("#typed").typed({ ...

Do colors appear differently on various screens?

I've noticed that the background color #1ABC9B appears differently on various monitors when viewing my website. On MAC systems and smart phones, it appears as a lighter shade of green compared to other laptops where it appears darker. What could be ca ...

Utilizing the optimal method for aligning text to either the right or left side

I am currently working on creating a container element with multiple child elements. I would like these elements to be aligned differently, some left-aligned and others right-aligned. This includes scenarios where the child elements themselves are containe ...

What is preventing me from running my alert script in JavaScript?

Need some assistance here. I have a simple question. I am trying to create an alert function using JavaScript, but it is not working. <script type="text/javascript"> $(".delete").click(function(){ alert("Are you sure?&q ...

How can we ensure that the borders of a text field automatically adjust to fit the dimensions of the text field using CSS?

I've encountered an issue with the borders of text fields while working on a project involving CSS/HTML. The problem is that in the browser, the borders appear shorter than the text fields. Initially, I attempted to adjust the border size to match th ...

Pattern matching for car license plate numbers that has to eliminate any special characters

I tried implementing this pattern, but it seems to allow special characters. How can I modify it to exclude them? ng-pattern ="/^([A-Z]{2,3}[0-9]{2}[A-Z0-9]{1,4}[0-9]{1,4})|([A-Z]{2}[0-9]{3})|([A-Z]{2}[0-9]{2}[A-Z]{1}[0-9]{1,4})|[^@]|$/" ...