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

Challenge with Karma runner running tests

Currently, I am in the process of developing an application that utilizes Karma as a testing tool to run test cases. Unfortunately, I have encountered an issue where the test cases do not execute and instead get stuck at a certain point. When attempting to ...

Positioning of Ajax modal popups on smaller screens

In my Asp.net/C# web application, I have a modal popup that contains multiple placeholders. However, I am facing an issue when trying to access the modal popup on smaller screens like iPads or iPhones. The page only displays half of the modal popup, maki ...

The AngularJS ngResource factory returns a function instead of a promise, providing a unique method for

Currently, I am working on developing an Angular factory that involves manipulating two sets of data. My objective is to make one set of data equal to a variable so that it can be passed in another API call. However, I'm encountering an issue where th ...

Delete a table row based on the values of checkboxes using Jquery

I've successfully generated an array from values selected in an HTML form using checkboxes. Is there a way to pass these selected checkbox values to jQuery and utilize the code $('tr#myTableRow').remove(); for each value in the array submit ...

Adding Hebrew characters from the URL query string to a field's content

I have encountered an issue while trying to extract a query string parameter value and inserting it into a field. The parsed output is not as expected. Here is the URL with the query string: activities.html?t=שלום Instead of displaying the value "ש ...

Ways to input a string into a field discreetly without displaying it?

I'm currently developing a Rails app that includes a form for users to input their age. When they enter `20`, I want to display it on the next page as `20 years old`. <%= form_tag age_path, method: "POST", id: 'age', remote: true do %> ...

Divs that are floated and only partially visible at the end

My layout consists of 6 Divs floated left to create 6 columns. The width of all these floats combined may extend beyond the window width for most users, especially with the 6th column included. Is there a way for this 6th column to be partially visible ( ...

select2 with customizable multi-column layout

Looking to create a multi-column select2 with a dynamic number of columns by specifying the number of columns as a data attribute in the select tag, or utilizing another method. <select data-cols="2,6,4"> <!--note data-cols--> < ...

Avoiding the use of angle brackets in XSLT语

Looking to replace a sequence containing '.\s+\w+' with a line break This is what I currently have: <xsl:value-of select="replace($fdesc,'[.][ ]+\w+','<br/>')" /> However, I am encountering an er ...

Vertically animating an image using jQuery

I have been experimenting with trying to make an image appear as if it is floating by using jQuery to animate it vertically. After some research, I stumbled upon this thread: Animating a div up and down repeatedly which seems to have the solution I need. H ...

Performing three consecutive Ajax requests in a Rails application

Recently, I developed a custom admin system for a local gym to manage payments, attendance, mailing, sales, and more. While everything is functioning smoothly, there seems to be an issue with the attendance feature. Occasionally, when taking attendance, an ...

What is the method for retrieving the fixed information?

I am currently working on a project that involves creating a course-rating API. We have been given a completed angular application to work with, and our task is to set up the routes without making any changes to the Angular app. One of the initial tasks a ...

What is the best method for inserting a dictionary into an input using AngularJS?

I've got a data dictionary in my controller that I'm currently showcasing using ng-repeat. The Title is the key, while the value is shown in the value field of an input. My goal is to allow users to alter these values and then submit the form. Ho ...

Achieve Perfect Alignment of Text Inside a Floating Div

Currently, I have a div aligned to the right inside another container. The text (date/time) within this right-aligned div needs to be vertically centered without using top padding. Whenever I apply top padding, it shifts my div downwards and disrupts its ...

Effortless script to make a URL request and display the response status | using JavaScript with jQuery

Is there a way to use JavaScript or jQuery to request a URL or website address and display the response code? For example: request www.google.com if (response_code = 200) { print "website alive" } else if (response_code = 204) { print "not found"; } ...

Tips for increasing the height of a popover when clicked

When a user focuses on the password input, a popover displays information. At the bottom of the popover, there is a link. How can I make the popover expand when the user clicks on this link? I have tried adding an !important class for the height value, us ...

Enhancing Highcharts: Extending Tooltip Functionality

My dataset contains a collection of data points that I want to display in a Highcharts chart. Here is an example of how the data looks: mydataset = [{ x: 1, y: 3, names: ["John", "Jane"] }, { x: 2, y: 4, names: ["Alice", "Bob"] }] ...

Obtaining data from HTML tags using Beautifulsoup

Here is the complete HTML code that I am currently working with. Below is a simplified version of the HTML provided above: <table class="premium"> <tr class="retailer top-offer" data-pricer="47.84" saler-id=" ...

Eliminate any empty spaces between the HTML tags

Is there a way to remove spaces between characters inside HTML tags? For example <option value="0" title="Advertising / Promotional Charges">Advertising / Promotional Charges</option> I tried using this code .replace(/\>\s+&bs ...

How can I deselect the 'select-all' checkbox when any of the child checkboxes are unchecked?

Here is the code provided where clicking on the select-all checkbox will check or uncheck all child checkboxes. If any child checkbox is deselected, the 'select-all' checkbox should also be unchecked. How can this be achieved? $(document).read ...