Tips for concealing (or revealing) a section of the information connected to a repeater through jQuery

In our database, we store details of various products. I want to display only the names of the products on a page without their associated details. When a user hovers over a specific product name, I would like the details for that particular product to slide down. The data is being retrieved and displayed using a repeater.

Below is the code snippet:

//html
    <div>
    <asp:Repeater ID="myRepeater" runat="server">
        <ItemTemplate>
            <div class="productTiltle">
                <%#Eval("producttitle") %> <br />
            </div>
            <div class="productDetail">
                <%#Eval("productdetail") %>
            </div>
        </ItemTemplate>
        <SeparatorTemplate>
            <hr />
        </SeparatorTemplate>
    </asp:Repeater>

//css
    .productTitle
    {
    background-color:#f00;
    font-size:x-large;
    text-align:center;
    }
    .productDetail
    {
    background-color:#ff6a00;
    font-size:large;
    }

//jQuery
    $(document).ready(function () {
        $(".productDetail").hide();
        $(".productTiltle").hover(function () {
            $(".productDetail").slideDown('normal');
        }, function () {
            $(".productDetail").slideUp('normal');
        }); 
    });

I want each product row retrieved from the database to be styled by the repeater. However, when I try to use jQuery to interact with the .productDetail class, it affects all elements with that class on the page. Is there a way to target a specific product using jQuery?

For example, if we have products numbered 1 through 100, with productTitles ranging from product1 to product100, and a user hovers over product23, only the details for product23 should be displayed.

How can this be achieved?

Answer №1

To achieve this, follow these steps.

.itemDetail
    {
       display: none;
       background-color:#ff6a00;
       font-size:large;
    }

Using jQuery:

$('.itemTitle').mouseover(function() {
    $(this).next('.itemDetail').slideDown('normal');
  }).mouseout(function(){
    $(this).next('.itemDetail').slideUp('normal');
  });

Answer №2

Have you experimented with using jQuery's hide and show methods?

Answer №3

To enhance your detail divs, add data attributes as follows:

<div class="productTitle" data-productid="<%# Eval("ProductID") %>">
    <%#Eval("producttitle") %> <br />
</div>

<div class="productDetail" data-productid="<%# Eval("ProductID") %>">
    <%#Eval("productdetail") %>
</div>

Next, use jQuery to activate the functionality:

$(document).ready(function () {
  $('.productTitle').click(function (e) {
      e.preventDefault();
      var productID = $(this).data('productid');
      $('.productDetail [data-productid="' + productID + '"]').slideDown();
  });
});

Please note that I corrected the typo from productTiltle to productTitle.

Answer №4

When it comes to styling, CSS is the way to go.

Having minimal JavaScript on your page not only reduces maintenance but also avoids headaches! Well-coded CSS will always outperform jQuery in this scenario.

HTML:

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
      <div class="wrapper">
        <div class="productTiltle">
            <%#Eval("producttitle") %> <br />
        </div>
        <div class="productDetail">
            <%#Eval("productdetail") %>
        </div>
       </div> 
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
</asp:Repeater>

CSS:

.productTitle
{
  background-color:#f00;
  font-size:x-large;
  text-align:center;
}
.productDetail
{
      background-color:#ff6a00;
  font-size:large;
  display: none;
}

.wrapper:hover .productDetail
{
  display: block;
}

Answer №5

Here is a helpful jQuery solution for you

<script type="text/javascript>
        // jQuery code snippet
        $(document).ready(function () {
            $(".productTiltle").each(function (index) {
                $(this).hover(function () {
                    console.log("slidd");
                    $(".productDetail").eq(index).slideDown('normal');
                }, function () {
                    console.log("slidu");
                    $(".productDetail").eq(index).slideUp('normal');
                });
            });
        });
    </script>

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

What is the most efficient method for sorting a list of nodes based on a specified array?

Below is the HTML code representing a list of elements: <div class="container"> <div class="apple-0">first-apple</div> <div class="apple-1">second-apple</div> <div class="apple-2">third-apple</div> ...

Creating a stylish login form with Bootstrap: How to effortlessly center it on your webpage

My login form is currently positioned to the left. However, I am trying to move it to the center of the page. I attempted some CSS styles without success. Interestingly, when I removed '{% extends "common/base.html" %}' from the code, the form di ...

Trouble presenting specific data values from JSON

Within my PHP file, I have the following code: $data = array(); while ($row = mysql_fetch_array($result, true)) {$data[] = $row;}; echo json_encode($data); This code generates the JSON array shown below: [ { "record_id": "4", ...

The web server is unaware of the CSS and JS references

After loading my ASP.NET MVC project on the web server, I encountered an issue where none of my CSS and JS references were loaded. Some of the references in my default layout are listed below: <head> <link href="/Scripts/css/bootstrap ...

Displaying two images side by side in HTML using Bootstrap

As a beginner in HTML, I am faced with the challenge of placing two images of different sizes side by side on the same row using HTML and Bootstrap. Below is my current code: <div class="row"> <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6"> ...

What is the best way to arrange 5 images in a single row using the bootstrap grid system?

I managed to recreate the following screenshot using Bootstrap 4.1: https://i.sstatic.net/fXaSt.png Check out this fiddle for my attempt, although the alignment of the images is not quite right. The HTML code I used in the fiddle to replicate the screen ...

How can I ensure my card div is responsive in Bootstrap?

My current section consists of only 6 cards, with 3 cards in a row of 2. When the screen size reduces (to mobile phone size), I want each card to be on its own row, displaying one by one as the user scrolls. Although it functions as desired when I resize ...

Tips for loading JavaScript and CSS files in a custom popup loader

Within the scope of my project, I have implemented a custom pop up page using JavaScript. This pop up includes a form for users to fill out. At the same time, I also need to load certain JavaScript files when the pop up is initialized. Can you please adv ...

What could be causing my web application to not reload upon opening?

My web app is in need of an update using JavaScript. Currently, it loads data from a website but for this demonstration purposes, "Hello World" fails to display. Although not the best code practice, it highlights the issues I am facing with more complex co ...

Setting the CSS position to fixed for a dynamically generated canvas using JavaScript

My goal is to fix the position style of the canvas using JavaScript in order to eliminate scroll bars. Interestingly, I can easily change the style using Chrome Inspector with no issues, but when it comes to implementing it through JS, I face difficulties. ...

Is there a better method for sorting tables using JQuery?

I'm currently populating a table using PHP AJAX and utilizing JQuery to implement column sorting functionality. Is there a more streamlined approach to coding this behavior? : $(document).on('click', '.thPartNumber', function() { ...

Adjust the file path using AJAX in PHP

I have been working on a unique library system that allows users to upload multiple files and then selectively post them. After the user uploads a file or files, they are stored in a temporary folder. Later, upon filling out a form, the user can choose a ...

To reveal the secret map location, just tap on the button - but remember, this only applies to

I am encountering an issue with duplicating a card and toggling the hidden location map. Currently, the location button only works for the first card and not for the rest. I need each card to independently open and hide the location map when clicked on the ...

tweetable fails to display any content within the div

I am attempting to showcase a Twitter feed similar to how it's done on Washio. They are utilizing both Tweetable 2.1.1 and Timeago 1.3.1, just like I am doing. As per the examples provided in the documentation for both plugins, this setup should func ...

What is the inability to place a div that is 30% wide alongside another div that is 70% wide, and keep them in a horizontal line?

If I make a div that is 30% wide, and another div that is 70% wide, they do not align horizontally. To fix this, I need to ensure that the combined widths of the two divs are less than 100%. Here's an example of how it can be done: <div id="wrapp ...

JavaScript classList.remove function experiencing inconsistencies

Take a look at this fiddle: JSFiddle The HTML code: <table class="myTable"> <tr> <th>Some text</th> <td class="align"> <span class=" someStyle">1</span>/<span class="otherStyle">15</span& ...

What is the best way to place a div below a div that is floated to the right?

HTML and CSS <div class="main-container"> <div class="left-panel"> <div class="panel-style">My Panel</div> </div> <div class="right-panel"></div> <div class="under-right-panel"></d ...

Tips for uploading an image file from Django Admin to the HTML

If I have already used a for loop in my code, how should I write the image address if I don't want to use 'for'? Can I directly write something like '/img/1.jpg'? Here is the directory where my images are stored: https://i.sstatic ...

Unable to retrieve information from external APIs using jQuery

I'm having an issue with fetching data from an external REST API using JQuery. The API returns undefined, but when I use my local REST API URL, it works fine. Can anyone help me troubleshoot this problem? Any code samples or explanations would be grea ...

Can a background image be displayed within a DIV using position:absolute?

I have scoured every corner, yet the answer still eludes me. In my style sheet, I have a DIV with absolute positioning and a background image. Within that DIV, I want to place a paragraph that appears underneath the background. I attempted to use z-inde ...