Manipulate container (div) to reveal and conceal

My jQuery was working fine until I added some more divs to my HTML. I'm now trying to toggle the opening and closing of a discussion container named discussionContainer with a click on the button replyButton. Any assistance would be highly appreciated.

I plan to add an ng-click attribute to the replyButton soon, as I need to pass an ID for an HTTP request to retrieve data from my database. Is there a way to achieve this using AngularJS or any other suggestions besides jQuery? The discussionContainer should start out closed when the page loads.

$(document).on("click", ".replyButton", function() {
  $(this).parent('.forQuestions').find(".discussionContainer").slideToggle(300);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="question">
  <div class="forQuestion">
    <div class="cardBody">
      {{answer.aContent}}
    </div>
    <div class="cardFooter">
      <div class="col-xs-4 footer1">
        <button class="replyButton">Reply</button>
      </div>
      <div class="col-xs-4 footer2">
        <i class="fa fa-trash" aria-hidden="true" ng-click="controller.deleteAnswer(answer._id)"></i>
        <i class="fa fa-pencil" aria-hidden="true" ng-click="controller.showEditAnswerModal(answer)"></i>
      </div>
      <div class="col-xs-4 footer3">
        {{answer.aDate | date: 'medium'}}
      </div>
    </div>
    <div class="discussionContainer">
      <ul>
        <li>comment1</li>
        <li>comment2</li>
      </ul>
      <div class="newComment">
        <textarea rows="2" cols="30" placeholder="New comment"></textarea>
        <button type="button" name="button">Comment</button>
      </div>
    </div>
  </div>
</li>

Answer №1

The issue in your code lies in the following:

1-- .parent() function only traverses one level up the DOM tree

2-- You have mistakenly used a class name as forQuestions, it should be forQuestion without the ending s. Therefore, you should utilize .closest('.forQuestion')

Demo

$(document).on("click", ".replyButton", function() {
  $(this).closest('.forQuestion').find(".discussionContainer").slideToggle(300);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="question">
  <div class="forQuestion">
    <div class="cardBody">
      {{answer.aContent}}
    </div>
    <div class="cardFooter">
      <div class="col-xs-4 footer1">
        <button class="replyButton">Reply</button>
      </div>
      <div class="col-xs-4 footer2">
        <i class="fa fa-trash" aria-hidden="true" ng-click="controller.deleteAnswer(answer._id)"></i>
        <i class="fa fa-pencil" aria-hidden="true" ng-click="controller.showEditAnswerModal(answer)"></i>
      </div>
      <div class="col-xs-4 footer3">
        {{answer.aDate | date: 'medium'}}
      </div>
    </div>
    <div class="discussionContainer">
      <ul>
        <li>comment1</li>
        <li>comment2</li>
      </ul>
      <div class="newComment">
        <textarea rows="2" cols="30" placeholder="New comment"></textarea>
        <button type="button" name="button">Comment</button>
      </div>
    </div>
  </div>
</li>

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

Determine the estimated download duration using the $http protocol

I am experiencing an issue with a function that calculates the time it takes to download a text file (3MB in size) from my server. While it works well for single requests, when I attempt to run multiple requests simultaneously, the time spent waiting for a ...

Enhanced browsing experience with personalized search suggestions

As a developer, I am aware that you have the ability to customize the browser's suggestions for various fields. For example, specifying that a field is for email and only suggesting values for email fields in the future. Is this functionality really ...

Obtain information using AJAX calls with jQuery Flot

Having an issue with jQuery Flot that I need help resolving. PHP output (not in JSON format): [[1, 153], [2, 513], [3, 644]] ~~ [[1, 1553], [2, 1903], [3, 2680]] Here is the jQuery call: $.ajax({ url: 'xxx.php', success: function (dat ...

What other choices are available for the Angular ui-select2 directive?

Within the Angular select2 controller below: <select ui-select2 id="projectListSelection" data-placeholder="Select a Project ..." ng-model="selectedProject"> @*ng-options="project.WebsiteName for project in projectList"*@ ...

Verifying the content of the JSON data

If I receive JSON data that looks like this: {"d":1} Is it possible to determine whether the value after "d": is a 1 or a 0? I attempted the following method, but it always goes to the else block, even though I know the JSON data contains a 1. success: ...

Tips on eliminating the gap between two flex items while adjusting their size

Can anyone assist me with my code? Here is the current version: https://i.stack.imgur.com/00RGC.png I want it to resemble this: https://i.stack.imgur.com/JSclD.png You can find all my attempted media queries in the CSS comments at the end. Please let m ...

Implement pop-up functionality on additional buttons. Modify existing code to accommodate multiple buttons

I have a feature that allows me to click on a button and trigger a pop-up window with the desired content. The issue I am facing is how to duplicate this functionality for multiple buttons, each triggering a different pop-up content. I attempted to duplic ...

Is it just me or does my wrapper always hover above the bottom of the screen?

I have been working on coding a simple layout that I created last year. Most of the work is done, but I ran into an issue where the 'wrapper' div doesn't extend to the bottom of the page as expected. It almost looks like it has the "position ...

Animating a CSS shape with the .animate method

I need help creating a dynamic graphic for my school project using css, jquery, and html. I want to make a rectangle that moves across the screen, but I'm having trouble getting it to work properly. Despite trying different variations of the animate f ...

Jquery: Transforming Content with Rotator or Slider - Seeking Answers

Hey there, I am currently in the process of revamping a website for a friend over at The current site was quickly put together using Joomla. Quick note: it might be best to mute your sound before visiting the site as there is an obnoxious video that auto ...

Exploring the drag-and-drop feature in Ruby on Rails version 3

Is there a way to replicate drag and drop actions in rspec request specs? In my scenario, I need to move the #divItem element to the #divContainer element using jQuery. After refreshing the page, I want to verify that the #divItem is now inside the #divCo ...

Encountering an EACCES error in Ubuntu when attempting to use bower for installation due to permission denial

When attempting to install bower, I used the command... sudo npm install -g bower I received the following output... npm http GET https://registry.npmjs.org/bower npm http 304 https://registry.npmjs.org/bower /usr/local/bin/bower -> /usr/local/lib/no ...

Hover over the Div element for inline effect

Hello all, I am currently working on running a while loop from a database to show different records. The twist is that these records are displayed as images, which makes it tricky to customize using standard CSS. To work around this, I'm implementing ...

Retrieving data with jSON through the local storage API

Seeking assistance with a problem I'm facing. Being new to this, the textbook Headfirst into programming isn't very helpful in explaining. After researching on stackoverflows, I'm still struggling. Any guidance would be greatly appreciated. ...

Centering content within a <th> tag

Hey there, I'm currently facing an issue with aligning a div inside another one. To illustrate the problem, I've set up a small fiddle which you can check out here: https://jsfiddle.net/jpq7xzoz/ The challenge arises when using the jQuery table ...

Utilizing HTML imports in Typescript for efficient use as TemplateStringLiteral

Recently, I started using TypeScript for the first time and I'm looking to integrate it into my webpack build. Currently, I am working with ts-loader and babel-loader to load TypeScript files while also attempting to include HTML files within the scr ...

How to attach input to function invocation in Angular 2

Can we connect the @Input() property of a child component to a parent component's function call like this: <navigation [hasNextCategory]="hasNextCategory()" [hasPreviousCategory]="hasPreviousCategory()" (nextClicked)="next ...

The offcanvas menu in the NextJS Tailwind website does not handle hover or tap events properly when outside of the parent dimensions

My header includes an off-canvas menu that slides in from the right side. Everything seems to be working fine, except for one issue: when the menu is open and visible, the mouse and touch events pass through it to the content underneath. Check out the cod ...

Two full screen divs aligned side by side using float:left

Looking for a way to create UL or DIV elements where each list item/child div takes up the full screen space (100% width, 100% height) and is floated left for horizontal scrolling. Here's my code, but it's not working as expected: HTML: <htm ...

jQuery implementation for a smooth image slideshow animation featuring seamless fading in and out of images

I am in the process of developing a unique image slideshow without relying on any plugins. My goal is to incorporate simple fade transitions, where one image fades out and the next one fades in seamlessly. The slider should function by appending images wit ...