Picking Out Specific Elements Sharing a Class in JQuery

Currently, I am working on developing a select state using two divs that are stacked on top of each other. One div is positioned relatively while the other is positioned absolutely with a bottom position set to -200px. When the relative div is clicked, the absolutely positioned div slides in displaying a "success" message.

Although this functionality is already implemented, I am looking to enhance it by removing the "success" div if the user changes their selection. Additionally, at the moment, clicking one div triggers all the divs to show the "success" state simultaneously. I am seeking a solution to rectify this issue without making any modifications to the html or css code.

You can find the JS fiddle for reference here: http://jsfiddle.net/LSan3/

 $(document).ready(function(){
  $('.main-div').click(function(){
    $('.inner-div').animate({
    bottom: "0px"
    }, 300 );
  });
});

Thank you!

Answer №1

Here is a solution that may meet your requirements:

$(document).ready(function(){
  $('.main-div').click(function(){
    $('.inner-div').stop().animate({
    bottom: "-100px"
    }, 300 );
    $(this).find('.inner-div').stop().animate({
    bottom: "0px"
    }, 300 );
  });
});

http://jsfiddle.net/LSan3/3/

Within the click function, we are first hiding all 'inner-divs' and then displaying the one related to 'this' - in this case, the 'main-div' that was clicked.

If this accomplishes what you were aiming for, please let me know.

EDIT: I have also included .stop() to prevent the animation from repeating multiple times if the user clicks the 'main-div' quickly.

Answer №2

Give it a shot:

$(document).ready(function () {
    $('.main-container').click(function () {
        $('.inner-container').animate({
            top: "-50px"
        }, 0);
        $(this).find('.inner-container').animate({
            top: "0px"
        }, 300);
    });
});

Check out the jsFiddle demo

Answer №3


 Give this code a try:

$(document).ready(function(){
 $('.container').click(function(){
  $('.content').animate({top: "-10px"}, 300 );

  $(this).find('.content').animate({
   top: "0px"
  }, 300 );
 });
});

Hopefully this solution works for you.

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

What causes a CSS class to have priority over another?

I've encountered a challenge in my Vue.js project where I'm utilizing Vuetify and vue-flash-message. My goal is to change the warning message background to 'blueviolet' by adjusting its style: .flash__message.warning { color: #ffffff ...

Utilize the Stripe Payment Gateway within your Cordova/Phonegap Application

I have spent a considerable amount of time searching for a solution. I am looking to incorporate Stripe Payment Gateway into my cordova application. Is there a method to achieve this on both Android and iOS using JavaScript? ...

Avoid choosing the menuItem when clicking on the icon within the Material UI select component

I am trying to create a dropdown menu that allows users to delete items. However, when I click on the delete icon within a menu item, the entire menu item gets selected automatically. How can I prevent this default behavior? Here is my code: { dropd ...

Searchbox aligned to the right using Bootstrap

Looking to place a searchbox on the right next to another container aligned left: <div> <div class="pull-left"><span>SOME TEXT</span></div> <div class="pull-right"> <div class="row"> ...

As I resize my browser window, I notice that my menu button starts to shift upwards along the

I recently created a menu button that looks great, but I noticed that when I shrink the browser window, it starts to move slightly upwards. It's really annoying and I can't figure out why this is happening. Can someone please help me troubleshoot ...

How can I configure knockoutjs for Danish culture?

When retrieving data from JSON, I use it to populate my Knockout.js viewmodel. Although the viewmodel successfully updates my input fields, the decimal values are in English culture format (1,900.50) instead of Danish culture format (1.900,50). Is there ...

The PHP script encounters an Ajax request and returns a null response

My current challenge involves sending data from the browser's local storage to PHP using AJAX. When I test the API in Postman by inputting this data (POST): [ { "product-id": "32", "product-name": "Reese Stevenson&qu ...

Nest is struggling to find solutions for the dependencies required by the UsersService

I'm currently working on implementing Authorisation in NestJs using Mongoose with a User Service. I've encountered some errors while trying to Unit Test: https://i.sstatic.net/iTAYJ.jpg I know that the UserModel needs to be injected into the Us ...

The Express server is set up with CORS enabled, however, it continues to encounter issues when attempting to make asynchronous HTTP requests from

We're currently experiencing an unusual issue and are seeking assistance to resolve it. Initially, our Express server is functioning well as an API when accessed from the same domain. However, we also need to utilize this API on our computers for tes ...

Place a <p> element at the bottom of a parent <div> that is floating

I am currently facing an issue related to the layout of my webpage. There is a @page media mentioned along with a floating <div> element placed at the top of the page. Inside this <div>, there are an <img> and a <p> tag (which conta ...

Using jQuery, locate the largest number within a specific class, add 1 to its value, and then input the remaining value into

Looking to create a unique reference number using jQuery (acknowledging it may not be the ideal method, but it's what I have to work with). Each existing number is added as a class and hidden on the page for this specific purpose: <div class="Ref ...

Revamp the color scheme of the majestic university WordPress theme

Struggling to update the color scheme on my grand college WordPress theme. Initially attempted through the back end with no success. Resorted to editing the CSS file located within a PHP file in the theme. Modified the line, uploaded it to the server, b ...

A guide on accessing objects from an array in Vue.js

Wondering how to choose an object from an array in Vue.js: When the page loads, the selectTitle() function is triggered. I simply want to select a specific object (for example, i=2) from my 'titleList' array. However, at the moment, I am only re ...

Compilation of geographic points within a database table

I am facing an issue with a JSON file structured like this: { "wijken": { "11": { "coords": "3.79073170001967,51.1717753664505,0 3.79020920176376,51.1723018883706,0 3.78989543642226,51.1729670713336,0 3.78983091856725,51.1736482209 ...

The reason why setting height to 0 is ineffective in a CSS definition

Within my current project, I am utilizing vue.js 2 and have implemented an img component. Below is the code for this component: <template> <div class="banner"> <img class="banner-img" :src="bannerImg"/> </div> </template> ...

Toggle the active class on the parent element when it is clicked

I'm encountering a problem with my JavaScript - attempting to make this function properly. Firstly, here is the desired functionality: 1.) Add or remove the 'active' class from the parent element when clicked 2.) When clicking inside the ...

The index on ref in MongoDB does not seem to be yielding any improvements

I've encountered a performance issue with my model: const PostSchema = new mongoose.Schema( { _id: mongoose.Schema.Types.ObjectId, workSpace: { type: mongoose.Schema.Types.ObjectId, ref: 'Workspace&apos ...

Using jQuery in Javascript, obtain the chosen radio option and place it within a label tag

I have a radio button for gender selection: <div class="label-inputs" name="userFieldDiv" id="genderUserFieldDiv"> <label class="required">Gender</label> <input type ...

An error occurs when trying to access the 'map' property of an undefined variable

Is there a way for me to retrieve the return value for this situation? I attempted to use forEach, but each time I try to loop through the images variable, I encounter the error ("Cannot read property 'map/forEach' of undefined") // Conso ...

Accessing a JBoss Web Service using JavaScript (AJAX) - A Comprehensive Guide

After experimenting with JBOSS's Web Services, I have successfully set up the following: http://127.0.0.1:8080/IM/TestService?wsdl My next challenge is to call Web Methods from that Web Service using JavaScript. For example, if there's a web m ...