Clicking on a div will toggle dynamically generated tables

I am facing an issue with toggling multiple tables created dynamically within a View using jQuery. Despite trying various methods to hide/show the div containing the table, only the first table is being toggled. While I have come across examples of applying toggle on table rows, implementing it for the entire table seems to be challenging.

HTML

<div class="col-md-10" id="main">
@foreach (var item in Model)
{ 
    <div id="ConfigName">
        <h3><b>@item.ConfigurationCollection.CollectionName</b></h3>
    </div>
    <div id="ConfigDetails" style="display:none">
    <table class="table">
        <tr>
            @foreach (var lis in item.ListConfiguration)
            {
                <th>@lis.Option.OptionName</th>
            }
        </tr>
        <tr>
            @foreach (var lis in item.ListConfiguration)
            {
                <td>
                    @foreach (var ov in lis.OptionValue)
                    {
                        @ov.OptionVal
                        <br />
                    }
                </td>
            }
        </tr>
    </table>            
    </div>
}

Script (Comments include everything I tried)

<script type="text/javascript">
$(document).ready(function () {
    $('#ConfigName').click(function () {
        //$('#ConfigDetails').slideToggle();
        //$(this).parent().find('#ConfigDetails').slideToggle();
        var closest = $(this).closest().find('#ConfigDetails').is(":visible");
        if(!closest)
        {
            $(this).find('$ConfigDetails').slideToggle();
        }

    });
});
</script>

Answer №1

There seem to be several issues at play here,

  1. The use of identical ids is problematic. It would be best to switch them to classes for uniqueness.
  2. In the line .find('$ConfigDetails'), it is advisable to utilize a period (.) instead of a dollar sign for class selection.

To resolve these issues, consider implementing the following code:

 $('.ConfigName').click(function () {
     $(this).next().slideToggle();
 });

Additionally, make sure to convert all ids to class names, including "configName".

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 necessary for background images to be loaded on mobile devices within media queries in CSS3?

Assuming the following CSS: div {background: #000} @media (min-width: 1000px) { div {background: url(myimage.jpg)} } Would a smartphone with a width of 320px download the background image? Thank you! ...

Altering the backdrop upon hovering over an element

As a beginner in Javascript and Jquery, I am working on creating an interactive feature where hovering over one element changes the background image in another column. I have managed to write the function, but now I want to add an animation to the image tr ...

Creating bookmarks using xhtmlrenderer in iText

Currently, I am utilizing xhtmlrenderer (commonly referred to as Flying Saucer) along with iText to transform HTML into PDF format. Could anybody provide me with guidance on generating bookmarks in this setup? A brief example would be greatly appreciated. ...

Issue with alignment on the printed version of an HTML document

While attempting to print an auto-generated HTML page through my Thermal POS printer, I encountered a large left margin issue. The application is developed using PyQt5, and I had to resort to a python script to generate the HTML code. Unfortunately, in the ...

Rotating Cursor during AJAX loading process

I utilize a WebGrid across many of my pages to showcase various products. Within the code snippet below, you can see how an item is added to the database when a user clicks on it: public bool ToCart(int userId, string partNumber, ...

Guide to dynamically loading data using jquery-localize

Currently, I am utilizing the jquery-localize plugin to handle translation on my webpage. My query pertains to the possibility of dynamically loading JSON data (received from the server) instead of relying on pre-existing files. This approach would greatly ...

The value coming from the Datalist is indeterminable, as Datalist is dynamically created through AJAX requests

In my HTML code, I have a Select element and a Datalist element. The Select displays options for different states like NY or NJ, while the Datalist contains a list of school names that are generated dynamically using Ajax with PHP because the schools depen ...

Has anyone successfully integrated parsley.js with a lightweight custom version of jQuery?

In my project, parsley.js is the only dependency that relies on jQuery. I am looking to minimize the impact of including jQuery by eliminating any unused jQuery modules. I attempted to create a custom version following the guidelines on the jQuery GitHub ...

Position the personalized radio button to be in line with the first choice text

I frequently update this section with a new Jsfidle link to demonstrate the issue when the max-width is set to 300px. For responsive design, adding the "span" tag before radio buttons helps align input content with custom checkbox backgrounds. To see the ...

Guide on sending an ajax request following a successful submission of a form using Contact Form 7, all while incorporating multiple tabs within Bootstrap 5

I have a scenario where I am utilizing multiple Contact Form 7 Forms within Bootstrap 5 tab panels. My goal is to trigger an Ajax request to send data after each successful form submission. To achieve this, I implemented the use of wpcf7mailsent event list ...

Modify the checkbox status on a webpage

Hey there! I'm feeling a bit lost when it comes to setting checkbox values on an HTML page. It's all a bit vague to me at the moment, and I'm hoping you can help shed some light on the matter. Here's what I'm trying to do: I want ...

Showing text and icons side by side in the mobile view

How can I align individual icons (created with WordPress) and text on the same line? It's displaying fine on desktop screens but not on mobile devices. Can someone please check the image attachment and suggest a CSS solution? Thank you! Best regards ...

I'm finding it difficult to decide which comparison operators to utilize for my JavaScript game. Experimenting with both the "&&" and "||" options has left me unsure of which

In this scenario: If the user enters a number in the text box, such as 2 (paper) or 3 (scissors), the "ComputerChoice" will randomly generate a number between 1 and 3. The goal is to make the computer understand and react to different scenarios... For exa ...

Customize blueimp-jQuery-File-Upload to upload files to a specific folder

My current project involves using the blueimp fileupload feature to upload images linked with specific property records. Each property's images are stored in a subdirectory of files that is identified by the record ID, such as files/1/img.jpg. In ord ...

What steps can be taken to turn off specific warning rules for CSS mode in ACE editor?

Utilizing the Ace Editor (through Brace and React-Ace) is a key aspect of my project. In our implementation, we specify the editor's mode as "css" and integrate it into our webpage. While this setup functions correctly, we have observed that some of ...

Utilize AJAX to showcase JSON data retrieved from a specified URL

We are striving to showcase the names listed in our JSON file within a div using JavaScript. Despite multiple attempts, we have not yet achieved success. JSON data: This is the approach we took: <button>fetch data</button> <div id="result ...

Struggling to effectively align the footer div in a responsive design

Check out this GitHub link to access the HTML file and CSS: https://github.com/xenophenes/pgopen-splash2016 I'm facing an issue with the footer text alignment. Despite my efforts, I can't seem to center it properly as it keeps appearing slightly ...

Using JQuery to reveal a hidden child element within a parent element

I'm facing a challenge with displaying nested ul lists on my website. The parent ul is hidden with CSS, causing the child ul to also remain hidden even when I try to display it using jQuery. One approach I've attempted is adding a class to the f ...

Manipulating div position interactively with vanilla javascript during scroll

I'm trying to create an effect where an image inside a div moves vertically downwards as the user scrolls, stopping only at the end of the page. I've attempted a solution using a script from another post, but it doesn't seem to be working. ...

What is causing my app.css to be reluctant in displaying the outcome?

Having an HTML page called chat.blade.php and a CSS file named app.css, I am facing an issue where the CSS changes are not reflecting on the page after running the PHP dev command. It might be possible that I have incorrectly written the path in the HTML f ...