Displaying a single item per click using jQuery

Hey there, I have a small issue with some HTML divs and show/hide functionality.

I'm trying to make it so that only one div is shown per click, but my current jQuery code is showing all the divs at once.

Any suggestions on how to fix this?

Check out the code here

jQuery(document).ready(function() {

    var toggleContent = jQuery('.insideContent');
    toggleContent.addClass('hidden');
    var thisElemnt = $(this);
    jQuery('.button').on('click', function() {
        thisElemnt.find('.insideContent').removeClass('hidden').addClass('show');
    });


});

jQuery(document).ready(function() {

  var toggleContent = jQuery('.insideContent');
  toggleContent.addClass('hidden');
  var thisElemnt = $(this);
  jQuery('.button').on('click', function() {
    thisElemnt.find('.insideContent').removeClass('hidden').addClass('show');
  });


});
.hidden {
  display: none !important;
}
.show {
  display: block !important;
}
.list {
  direction: inherit;
  display: flex;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="list">
  <div class="col-lg-2 workImage">
    <div class="itemTitle">
      <h2 class="page-header  text-center">Item</h2>
      <button class="button">Open Project</button>
    </div>
    <div class="insideContent">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </div>
  <div class="col-lg-2 workImage">
    <div class="itemTitle">
      <h2 class="page-header  text-center">Item</h2>
      <button class="button">Open Project</button>
    </div>
    <div class="insideContent">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </div>
  <div class="col-lg-2 workImage">
    <div class="itemTitle">
      <h2 class="page-header  text-center">Item</h2>
      <button class="button">Open Project</button>
    </div>
    <div class="insideContent">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </div>
</div>

Answer №1

jQuery(document).ready(function() {

    var toggleContent = jQuery('.insideContent');
    toggleContent.addClass('hidden');
    var thisElemnt = $(this);
    jQuery('.button').on('click', function() {
        $(this).closest('.itemTitle').next('.insideContent').toggleClass('hidden show');//apply the click element only
    });


});

Utilize .closest() to locate the nearest parent container with a class of .itemtitle, then utilize .next() to target the following div, and finally use .toggleClass() to display or hide the targeted div.

VIEW DEMO

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

Is it possible for the ".filter" function to verify if the target contains multiple attributes?

I'm currently working on a checkbox filter setup and using Jquery's .filter to sort through some divs. Below is the snippet of Jquery code that I am using: $(document).ready(function(){ var checkboxes = $('div.filter-groups').find(&ap ...

What is the best way to add a textfield within an image?

Does anyone have a code snippet that can generate a "search box" like the one on www.dx.com? I was shocked to find that searching for "textfield inside image" only yielded results for "image inside textfield". ...

JavaScript function is not identifiable

I am currently developing a tic-tac-toe game with 9 buttons that will display either an X or O image depending on the boolean value of the variable isX. The main container for these buttons is a div element with the id 'stage' and each button ha ...

What is the best way to retrieve a value in Ajax?

I'm trying to assign the result of an MVC controller method to a variable. function someFunction(){ var result; $.Ajax{ //??? } return result; } //Contrast with C++ int f() { //just! return result; } Post Script: Th ...

Is there a Jquery function that retrieves the html content of a specified div element?

After trying multiple approaches without success, I am struggling with extracting the HTML content of a div element in my HTML layout. The div element is declared as ("myDiv"). I am looking to create a function that can accept any div as an argument and th ...

Issues with hidden overflow and height attributes not functioning correctly in Internet Explorer 9 when using the DOCTYPE HTML standards mode

I have a <div> with overflow:hidden and height:100% that adjusts the div contents to fit the window's height on Chrome and Safari. However, in IE9, the div does not respect the styles of overflow:hidden and height:100%, causing it to expand to ...

Issue with MVC 4 Asynchronous File Upload: Controller Always Receiving Null Value

I'm encountering an issue while attempting to upload a file asynchronously from ajax to my controller. I am passing 3 variables - PictureId, PictureName, and PictureFile. The problem specifically lies with the "PictureFile" variable as it consistently ...

Trigger the datepicker to open by clicking anywhere within the TextField in MUI

I am trying to enhance my date picker functionality by displaying it when users click anywhere within the field, not just on the calendar icon. Below is the picker: https://i.stack.imgur.com/mrAui.png export function DatePickerField(props) { ...... ...

What is the most efficient way to handle dependencies and instantiate objects just once in JavaScript?

I am interested in discovering reliable and tested design patterns in Javascript that ensure the loading of dependencies only once, as well as instantiating an object only once within the DOM. Specifically, I have the following scenario: // Block A in th ...

Tips for customizing the border radius style of the menu in Vuetify's v-autocomplete component

I am looking to customize the appearance of the drop-down list in the v-autocomplete component by adding a border-radius style, as depicted in the image below. The current design I have achieved closely resembles the visual shown below. Previously, I app ...

Step-by-step guide on utilizing jQuery to fade out all elements except the one that is selected

There are multiple li elements created in this way: echo '<ul>'; foreach ($test as $value){ echo '<li class="li_class">.$value['name'].</li>'; } echo '</ul>'; This code will generate the fol ...

Tips for customizing the appearance of the react-stripe-checkout form

Could someone please provide guidance on applying custom styles to the react-stripe-checkout form component, such as changing the background color? Visit this link for more information ...

arrange a div inside another div

I'm struggling to grasp the concept of how divs function in HTML. Below is a snippet of my HTML code: <div id="user_p"> <img src="img/pp/djbaptou.jpg"> <div class="followings"> djbaptou </br> Baptiste Arnaud </br> ...

Exploring nodes with the help of Cheerio JS

In my HTML code, I am using the Snekfetch library to fetch data. Here is a snippet of the code: <div class="entry-content"> <h4>today's date etc etc</h4> <h3>category name 1</h3> <p> <img class=" ...

Adding dynamic images to Bootstrap Popover on the fly

Does anyone have suggestions on how to effectively retrieve the image from the data-img attribute in order to use it for a popover displaying product images? Thank you! <a href='#' class='toggle-popover' title='Hello World&apos ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

Execute jquery commands after the function has finished

I am trying to implement the code below: $(":file").change(function () { if (this.files && this.files[0]) { console.log(this.files[0]); var reader = new FileReader(); ...

Issue encountered while trying to modify the color of a placeholder

Whenever I attempt to modify the placeholder's color: ::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #909; opacity: 1; } : ...

Working with CSS3 transitions in PHPStorm is a breeze

For some reason, my phpstorm does not provide code completion for CSS properties like -webkit-translate, translate, or animation. Has anyone encountered this issue before? Any suggestions on how to fix it? ...

Tips for configuring scroll on Jquery Gantt Chart to focus on current date

Currently, I am utilizing the jQuery Gantt Chart. My goal is to have the default scroll position set to today's date. At the moment, it seems to start from the earliest recorded date instead of today's date. I would greatly appreciate any assista ...