What is the best way to choose a child element that has been clicked using jQuery?

How can I make only the child of the clicked element slideToggle? I tried implementing a solution, but it ended up sliding all divs with the .description class. Can anyone provide some guidance?

HTML:

<div id="container">
<div class="row">
  <div class="item"> <div class="toggle"></div>
    <div class="description"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet  </div>
  </div>


  <div class="item"> <div class="toggle"></div>
    <div class="description"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea c </div>
  </div>


  <div class="item"> <div class="toggle"></div>
    <div class="description"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, </div>
  </div>
</div>

jQuery:

$(document).ready(function(){
  $(".toggle").click(function(){
    $(this).parent().find(".description").slideToggle();
  });
});

CSS:

.row{
    width:900px;
}
.toggle{
    width:150px;
    height:200px;
    margin:0 auto;
    background:red;
}
.item{
    float:left;
    width:300px;
    text-align:center;
}
.description{
    display:none;
    text-align:left;
}

Answer №1

An alternative method is to utilize the .next() function in jQuery to locate the next sibling element

$(document).ready(function () {
    $(".toggle").click(function () {
        $(this).next(".description").slideToggle();
        //$(this).sibling(".description").slideToggle();
    });
});

Check out the demo here: Fiddle

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

Do we really require a mime type to play videos in HTML5?

I recently stumbled upon an interesting observation while coding a video player using HTML5. Surprisingly, I found that my code runs smoothly even without specifying the type attribute (which defines the mime type) in the source element as shown below. Co ...

Discover the identity of an element through the value of another element

Looking for a way to retrieve the id value based on the input value in this HTML code. For instance, I need to identify the id value associated with the input value of number2 - which is unknown2. How can this be achieved using jQuery? <th id="unknow ...

Prevent a div from being displaced by the transform property once it reaches the window's border

Is it possible to prevent the viewer I created using CSS transform from moving when its borders reach the window borders? Should I consider using a different method instead? If you'd like to see the code, you can check it out here. var x=0, y=0 ...

Achieving functionality with dropdown menus in jQuery

I am facing an issue with a dropdown menu that works perfectly in jsFiddle during testing, but does not function as expected when I run it on my testing server. jsFiddle: http://jsfiddle.net/cyberjo50/39bu8/2/ HTML <!doctype html> <html> < ...

Could you please explain the mysterious space that appeared between padding-bottom and border-bottom?

I noticed a very subtle orange gap between the padding-bottom and border-bottom. What could be causing it? https://i.sstatic.net/7eFXw.png Below is the code snippet in question: p { border-bottom: 1px solid #E8E8E8; margin: 0 auto; padding-botto ...

How can I extract values from an array stored in an external text file using Jquery?

I'm attempting to retrieve an error message from an external file called errors.txt. The contents of errors.txt are as follows: var data = { 'errors': [{ "code": "9999", "message": { row: 1, ...

Add a string to the beginning of the JSON data before displaying it

Upon receiving JSON data through an AJAX call, the format of the data is structured as follows: data[0].scores.sadness The term 'sadness' in the above example represents one of the eight emotions. Instead of printing out individual emotions lik ...

How should a frame be correctly inserted into a modal in Bootstrap?

I'm currently working on incorporating a frame into a CSS modal, but the current display is not quite ideal. Additionally, I manually adjusted the frame's position (top and left), which is causing misalignment issues for different screen sizes, r ...

Tips for displaying an absolute div component on top of a table with a scroll bar visible

I am trying to work with an HTML table that has a scroll feature. When a cell is clicked, a custom component opens. However, I am having trouble getting the custom component to scroll as well. Take a look at my jsfiddle here:: https://jsfiddle.net/rujz69n ...

Challenges with displaying content in Bootstrap tabs

I am currently working on the following code: HTML <div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" role="tablist" id="tabs_list"> <li class="nav-item"> <a class="nav-li ...

javascript alter css property display to either visible or hidden

I am struggling with a CSS id that hides visibility and uses display: none. The issue arises when I want the element to be visible upon clicking a button, but removing the display:none property is causing design problems due to it being an invisible elemen ...

jQuery causing trouble with AJAX in Rails

Currently, I am fetching a list of users from the controller side and generating the HTML code to append it. This is how I wrote the code: $.ajax({ type : "get", contentType : "application/json; charset=utf-8", url : "/users/sear ...

Setting the button value to a textbox and refreshing the status information (Codeigniter)

How do I pass the value of my "ACTIVE" status attribute to my textbox? I want to update the user's status by clicking the ACTIVE button. The user's status is currently pending. While I can easily pass the userID, I'm facing an issu ...

PHP loop that generates gaps between the buttons

My current challenge involves using a while loop to dynamically generate buttons based on the `outcomeId` count. However, I am facing an issue where the buttons created by the loop have gaps between them. <div class="outcomes"> <?php ...

"By clicking on it, change the href attribute to a new source

I'm currently working on a project that involves swapping thumbnail images to a larger image when the thumbnail is selected. It's functioning as expected, but now I want to allow end users to click on the larger image to display it in jquery.ligh ...

Location of Ajax callback function

I encountered an issue with a callback function that was placed within $(document).ready. The callback function wasn't functioning as expected. Surprisingly, when I moved it outside of $(document).ready, the code started working flawlessly. I am puzzl ...

Ajax sends the URL location to Python

I'm attempting to piece together some code. There are two distinct functions that I am trying to merge into a single entity. Code snippet: <!DOCTYPE html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> &l ...

Utilizing dxautocomplete in a dynamic manner

Friends I have implemented dxautocomplete in devextreme mobile dxlist. However, only the first line is being generated. I am looking to customize each line with different data dynamically. Although the same code repeats in the section below, I need guidan ...

Problems encountered with the ajax contact form embedded in a WordPress theme

I have encountered an issue with a website I am trying to troubleshoot. The form on this particular page: appears to be malfunctioning... The form is failing validation and not sending any emails. The expected functionality is for the form to validate fi ...

Tips on showcasing information with ng for in Ionic 2

https://i.sstatic.net/MKIcJ.jpgI am currently working on the thank you page for my project. When the success call is made, it will display both ORDER DETAILS and Delivery Details. However, in the event of a failure call, only the ORDER DETAILS are displaye ...