Fade out adjacent elements if currently visible using jQuery

Currently, I am attempting to devise a method to target all siblings and gradually fade them out before fading in the specific element that is being targeted.

$('#MyDiv').siblings(':visible').not('h2').fadeOut('slow', function() {
    $('#MyDiv').fadeIn('slow');
});

Even though the :visible selector should work in theory, it doesn't seem to be functioning properly. The main issue arises when the complete event triggers immediately due to some siblings already being hidden.

I believe there must be a straightforward solution for this problem, but perhaps I have been gazing at it for too long and missing a simple detail.

Answer №1

Let's experiment with a new strategy for this problem. It seems that the "complete" event is only triggering once in this scenario.

$('#div3').fadeOut(500);
$('#div1').fadeOut(500);

setTimeout(function() {
  $('#div1').siblings(':visible').not('h2')
    .fadeOut(500).promise().done(
      function() {
        $('#div1').fadeIn();
      }
    );
}, 1500);
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>

  <div id="div1">
    <p>Div1</p>
  </div>
  <div id="div2">
    <p>Div2</p>
  </div>
  <h2>H2</h2>
  <div id="div3">
    <p>Div3</p>
  </div>
  <div id="div4">
    <p>Div4</p>
  </div>

  <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
</body>

</html>

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

Update the knockout values prior to submitting them to the server

Imagine having a ViewModel structured like this with prices ranging from 1 to 100... var Item = { price1: ko.observable(), price2: ko.observable(), price3: ko.observable(), ... ... price100: ko.observable(), name: ko.observable ...

how to position one div above another using css

I currently have a page layout with 7 different div elements structured like this; <div id="container"> <div id="head"></div> <div id="A"> <div id="A1">A1</div> <div id="A2"></div> ...

Tips on how to toggle/close a dropdown menu that is displayed on a different page using Vue

Below is a screenshot of my cart dropdown: https://i.sstatic.net/IdOZK.png The issue I am facing is that the cart stays open even when I navigate to another page. I need it to close when I move to a different page. Below is my component code: <template ...

Unable to detect any errors in the CSS file

Using RSpec to write some feature tests, I encountered an error in my application where a pre-purchased template with numerous styles is causing issues. Upon running the specs, the following error occurred: ActionView::Template::Error: Invalid CSS ...

Display the icon exclusively when hovered over

I have a table with a column containing icons that I want to hide by default. I would like the icons to only appear when hovered over. Here is the HTML code: <table class="table table-hover"> <tr> <th>From</th> <th> ...

Preventing page reload using JavaScript function

I am currently encountering an issue with my code. The situation involves pulling items from a JSON database and displaying multiple items on the page. I have implemented a class called showProduct, which triggers when an item is clicked. This class execut ...

Using JavaScript regular expressions for email validation criteria

Hey there, I am struggling with Regular Expressions, especially when it comes to client side validation for a specific field. Can you please help me come up with a Regular Expression that would verify if an email address is valid based on these criteria: ...

Adding Custom CSS File to an iFrame in Angular

Currently, I am attempting to include a CSS file in a third-party iframe to modify its styling. My initial idea was to employ the following code snippet: ngOnInit() { ... this.myIframe.nativeElement.document.head.appendChild('style.css') } U ...

Verify the accuracy of the color value on a web page using Selenium in Java

As a beginner in using Selenium with Java, I encountered an issue related to a symbol in my connected class turning green. I needed to assert whether the symbol actually changed its color. Below is the code snippet that I used: Boolean connectionSymbolG ...

What allows me to modify the border color and thickness of my table's <thead>, but prohibits me from changing the background color?

I have successfully changed the border styling of the top bar in the table, however, I am struggling to change the background color no matter what I try. Can someone point out where I might be making a mistake? Here is my code: <table> <thead> ...

Stop unauthorized access to JSON Web Service

I have a unique webservice that involves inserting a specific piece of JavaScript code onto the webpage. This JavaScript is responsible for scanning the page in search of a particular string, triggering a call to my ASP.NET JSON WebService. The resulting J ...

different ways to dynamically increase CSS ID in PHP

Is there a way to dynamically increment my CSS ID using PHP? foreach ($query_cat->result() as $row_cat) { $row_cat_id = $row_cat->id; echo '<div class="product-wrapper">'; echo '<div id="product-header' . $r ...

Guide to effectively utilizing addEventListener for images

Is it possible to measure the width of an image loaded by user click in Javascript? I need the width in order to adjust the position of a button next to the image. Here is the code I am using: document.getElementById('preIMG').addEventListener( ...

Increase the size of the Bootstrap select input area to exceed the dimensions of

Is there a way to ensure that the selection area of my bootstrap select is not larger than its parent container on small screens (mobile)? My issue can be seen in the image below and is recreated in the snippet provided. The black bar represents the edge ...

Adding a class name to the three active, visible, and shown slides in SwiperJS is a simple process that can be done by

Currently, I am working on customizing a carousel in Swiper with slidesPerView={3}. My goal is to style the sliders that are not visible on the screen. I have made multiple attempts to achieve this: First, I tried adding the attribute slideActiveClass="sw ...

Ways to stop a named anchor from causing a line break

Looking at the code snippet below: <a name="top"></a> <div class="topbar"> <img src="banner.jpg" alt="The Group Company" width="100%" /> <div class="printOnly"> <center><b>Printed from www.company.com</ ...

``When you click, the image vanishes into thin

Could you please explain why the image disappears once I close the lightbox? Access with password: chough ...

My JavaScript code is functioning properly in jsfiddle, but when I try to run it on my own website, I encounter

Encountered an error message stating "Uncaught typeError: cannot call method 'hover' of null" following the line $('#nav li a').hover(function(){ in my JavaScript code. Check out the code on my site here: http://pastebin.com/GjZBEu3s Y ...

The issue with additional parameters not functioning in jQuery autocomplete

JavaScript Issue $('input.completeme').each(function() { $(this).autocomplete({ source: function(request, response) { $.ajax({ url: '<?=base_url()?>patients/autocomplete', data: { & ...

An unexpected character caused an error during JSON Response processing

My web service is returning the following Response/Payload: [{"msg":"Order requires backorder","status":"ERROR"}] Below is the AJAX code I am using to handle this response: $.post("/myorder/{{ order_id }}", {}, function(data, status){ ...