The JQuery Animate() method is capable of executing animations even on elements excluded by the .not(), .not('...'), :not(...) and the :not('...') selectors

I had assumed this would be a simple issue, but after searching through the forum, I couldn't find a solution.

From the title, you can tell what's wrong. My code is mistakenly hiding the #player-one-turn element within the parent element #fourth-step (although every element belongs to the body element). I'm hesitant to modify the DOM structure. Could it be necessary? I'm not sure.


$("#fourth-step").not($('#player-one-turn')).animate({
  opacity: 0
});

// SAME AS ABOVE - DOESN'T WORK
//$("#fourth-step:not('#player-one-turn')").animate({ opacity: 0 });
//$("#fourth-step:not('#player-one-turn')").animate({ opacity: 0 });
//$("#fourth-step:not(#player-one-turn)").animate({ opacity: 0 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fourth-step">
  <div id="player-one-turn">
    DONT HIDE ME PLS
  </div>
  asd asd as das das das d asd as
</div>

For better editing, here is the JsFiddle link: https://jsfiddle.net/ax07dnrf/1/.


Multiple attempts were made with no success:

$("#fourth-step").not($('#player-one-turn')).animate({ opacity: 0 });
$("#fourth-step:not('#player-one-turn')").animate({ opacity: 0 });
$("#fourth-step:not('#player-one-turn')").animate({ opacity: 0 });
$("#fourth-step:not(#player-one-turn)").animate({ opacity: 0 });

Could there be a syntax error? Unlikely :D


Similar threads didn't provide a working solution for my problem, despite trying both :not() and .not():

  1. Select all body except one element
  2. jquery - run function on each element except the one that was clicked

Answer №1

Your instructions are to locate the element with the id of fourth-step and then exclude the element with the id of #player-one-turn. However, the element #player-one-turn will not be included in the initial selection by the first selector, so it cannot be excluded.

To achieve the desired result, you should target all the child elements within fourth-step and exclude #player-one-turn from those specific results.

The plain text asd asd as das das das d asd as is not considered a child element within the parent element and therefore, is automatically excluded. If you enclose it within an HTML element (such as span), it will be included in the selection.

// It is not necessary to pass a jQuery object to .not(), just a selector
$("#fourth-step").children().not('#player-one-turn').animate({
  opacity: 0
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fourth-step">
  <div id="player-one-turn">DONT HIDE ME PLS</div>
  <div>HIDE ME PLS</div>
  <div>HIDE ME PLS</div>  
  asd asd as das das das d asd as
</div>

Answer №2

Utilize the find() function along with implementing it to achieve desired results. For a deeper understanding, check out .not()

$("#fourth-step").find('*').not('#player-one-turn').animate({
  opacity: 0
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fourth-step">
  <div id="player-one-turn">
    DONT HIDE ME PLEASE
  </div>
  <span>asd asd as das das das d asd as</span>
</div>

Answer №3

$("#fifth-step").contents().not($("#player-two-turn")[0]).wrap("<section></section>").parent().animate({
  opacity: 1
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="fifth-step">
  <div id="player-two-turn">
    SHOW ME IF YOU CAN
  </div>
  qwe rty uio pas df g h jkl
</div>

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

"Combining background images with javascript can result in displaying visual elements

Hello! I am in need of assistance with a CSS + Javascript fog effect that I have developed. It is functioning properly on Firefox, Opera, and Chrome but encountering issues on IE and Edge browsers. The effect involves moving two background images within a ...

Is there a way to remove this illicit JavaScript character from the code? It is appearing during execution

I am in the process of creating a custom tooltip using Microsoft Chart Controls. These controls provide support for using keywords to automate the data you want to display. For instance, string toolTip = string.Format("<div> {0}: {1} {3} ({2}) ...

Implement a Horizontal view feature in the FullCalendar API

In my project, I am utilizing the full-calendar API to create a vertical resource view. Everything works perfectly when I have around 10 resources. However, once I increase the number of resources in my project, the view becomes distorted. https://i.sstat ...

Revamp nested .map calls to utilize async/await pattern

Currently, I am working with a JSON file structured similarly to the example below: [ { "Item": "Item1", "ItemChild": [ { "Category": "Category1", ...

Locate the XPath for text that is within a div tag and adjacent to a span tag

I've searched, but couldn't find an exact match, so my question is about a low HTML code: <div class="column1"> <div> <div class="name"> Dynamic Name <span class="id" title="ID">Dynamic ID</span> </div> </d ...

What's the Deal with VueJS and WebRTC: Troubleshooting Remote Video Playback Issues

For a sample application I am developing, I need to incorporate two video elements and a "Call" button. The first video element (#localVideo) will display the local media stream output. Upon clicking the call button, the remote video element should play th ...

What is the best way to display my table?

In the index.php view, you will find my table located <table class="striped"> <thead> <tr> <th>Id</th> <th>Name</th> <th ...

Perform an AJAX request to an external server from a Microsoft Office add-in

Having trouble with a jQuery GET request in my Office (Outlook) add-in. Getting the error message "Access denied". The documentation is scarce and hard to locate here. Any ideas on what might be causing the issue? ...

Mounted class not initiating transition in Vue.js

After attempting to apply a class to an element in the mounted lifecycle, I noticed that the transition effect was not taking place. The element would immediately display in its final state: However, when I used setTimeout to delay the class change, the t ...

Unfortunately, I am unable to utilize historical redirection in React

When an axios request is successfully completed, I want to redirect. However, I am encountering an error that looks like this: https://i.sstatic.net/irTju.png Below is the code snippet: import React, { useState, Fragment } from "react"; import S ...

Type in data into the database in real-time

Can someone help me with inserting data into a database while typing values in a textbox? My AJAX code doesn't seem to be working and I'm not getting any errors. Please assist me as soon as possible. Here is my AJAX code: var userid = '#u ...

Is it possible to store data using kue in a node.js environment?

I am currently working with the kue and node.js framework. Check out kue on GitHub When I create and save a job, the data is stored in my local redis server. var job = queue.create('new_job', { title: 'welcome email for tj' ...

What could be the reason for my typeahead.js not showing the dropdown menu during an ajax request?

I am currently experiencing an issue with the typeahead.js autocomplete in my Laravel application when using AJAX as the data source. The suggestion menu does not display when using AJAX, but it works perfectly fine when the data is retrieved from a div co ...

Incorporate a background image into input fields with Autofill on mobile browsers to override the default yellow background that obscures them

It is common knowledge that modern browsers apply a less than appealing yellow background to text fields when Autofill is used by visitors. A helpful workaround is known to override the default text and background colors, and even incorporate a background ...

Connected selection menu

I have noticed several discussions on this topic, but many of them rely on JavaScript or focus solely on a standard drop-down list. I have developed a PHP function that generates drop-down menus based on select queries in my database. Currently, this part ...

the text input remains fixed in size and does not resize

Visit the page and scroll down to find a section located next to the "Directions" button that contains an input field. This input field allows you to enter an address and utilize Google Maps for navigation. One puzzling aspect is that regardless of what ...

fetch and modify data simultaneously in firebase

Is there a way to retrieve a value from a Firebase document and immediately update it? I am familiar with methods for updating documents and retrieving their values separately, but how can I accomplish both in one go? update firebase.firestore().collecti ...

Adjusting the desktop view through media queries for mobile devices

As someone with no coding experience, I've been working on my portfolio using Cargo. Currently, I'm trying to adjust the mobile view of an article on my homepage through media queries. However, the code is unintentionally changing the layout on d ...

The infinite scroll feature is firing the AJAX request successfully, however, the next paginated page is not being displayed

Currently, I am working on a Rails app and utilizing will_paginate for pagination. In order to implement an infinite scroll feature, I decided to base it on will_paginate. However, after multiple attempts, the request seems to be functioning but instead of ...

Error: Attempting to access values from an undefined object (value: '0') while summing arrays with useState

const [lnames, setlNames] = React.useState(); const [lnums, setlNums] = React.useState(); React.useEffect(() => { axios.get("http://localhost:7001/lunch").then(response => { let arr1 = []; let arr2 = []; ...