Tips for displaying multiple table rows when clicked

Looking to display two hidden rows at a time out of a total of 5 rows when a display button is clicked. Once the last 2 rows are displayed, the button should become disabled.

Similar to a question on Stack Overflow, but unable to use the solution provided due to the css affecting the data and styling within the rows.

<p><a href="#" id="display">Display More</a></p>

<table>
 <tr class="showfirstRow">
  <th>First Name</th>
  <th>Last Name</th>
</tr>
 <tr class="secondRow">
  <td>Peter</td>
  <td>Griffin</td>
</tr>
<tr class="thirdRow">
  <td>Lois</td>
  <td>Griffin</td>
</tr>
<tr class="fourthRow">
 <td>Meg</td>
 <td>Griffin</td>
</tr>
<tr class="fithRow">
 <td>Stewie</td>
 <td>Griffin</td>
</tr>
</table>

$("#display").click(function() {
// show the next hidden div.group, then disable the display button once all rows have been displayed
});

Answer №1

Implemented a new hidden class to facilitate element hiding functionality.

To reveal the next set of hidden elements within a table, you can utilize the slice(0,2) method, adjusting the number to your preference. Remove the hidden class from these elements. If there are no more concealed elements left in the table, apply the hidden class to the button that triggered the action in order to hide it.

For demonstration, refer to the following example:

$("#display").click(function() {
  $('#tblWithHiddenRows .hidden').slice(0, 2).removeClass('hidden');
  if ($('#tblWithHiddenRows .hidden').length == 0) {
    $(this).addClass('hidden');
  }
});
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><a href="#" id="display">Display More</a></p>

<table id="tblWithHiddenRows">
 <tr class="hiddenfirstRow">
  <th>First Name</th>
  <th>Last Name</th>
</tr>
 <tr class="secondRow hidden">
  <td>Peter</td>
  <td>Griffin</td>
</tr>
<tr class="thirdRow hidden">
  <td>Lois</td>
  <td>Griffin</td>
</tr>
<tr class="fourthRow hidden">
 <td>Meg</td>
 <td>Griffin</td>
</tr>
<tr class="fithRow hidden">
 <td>Stewie</td>
 <td>Griffin</td>
</tr>
</table>

Answer №2

Use the data-num attribute within the following table row code:

<tr class="row secondRow" data-num="1">

Next, incorporate the provided script:

num = 1;

$("#display").click(function() {
    row1 = $(".row[data-num='"+num+"']");
  row2 = $(".row[data-num='"+(num+1)+"']");

  if(row1.length>0)
    {
    row1.show();
        row2.show();
  }
  else
    $(this).addClass("disabled");

  num+=2;
});

Visit the following link for further reference

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

Extra brackets appear when retrieving JSON data

In my code, I am attempting to extract data from a JSON file. The JSON data does not have any brackets, just an array separated by commas. However, when I retrieve the data using $scope, an extra square bracket is added outside of my output. Here is a demo ...

Implementing a permanent class following an incident

Is it possible to dynamically add a class to an element when an event occurs rather than based on a condition? For example, I have a td that displays a variable. <td>{{myVar}}</td>//myVar=10 When the variable changes to myVar=15, I want to ad ...

File-loader mistakenly generates 2 images and associates them with the incorrect one

I have encountered an issue with file-loader where it is creating two separate images in my build folder. One image is named correctly and saved in the correct path, while the other one is named [hash].png (default) and is stored directly in the build dire ...

CSS Issue: Hovering Over Link Causes Text to Shift in Internet Explorer

Encountering a problem with CSS and IE, specifically in Internet Explorer 9. When hovering over certain links, the text shifts down which does not occur when using Firefox. You can see an example of this issue at: http://www.debtdispatch.com/list_item_err ...

Error in Angular timer causing NaN result

Struggling to create a timer in JavaScript, a basic one at that. I wrote what I thought was correct code, but it's not functioning properly - resulting in the textbox value changing to NaN after just one second. Below is the code snippet: <timer ...

Strange alignment issues occurring solely on certain PCs

Currently, I am in the process of developing a website for a client who has requested that it be an exact replica of the mockup provided. However, I have encountered some issues with the headers and certain divs that contain background elements. Surprising ...

Utilize jQuery to target elements containing more than one class

Suppose an element has several classes as shown below: class="btn btn-primary add-movie-button is-on" Is it possible to select this element using only one class name with jQuery, for example: $(".add-movie-button") Can the method .hasClass("is-on") be ...

When attempting to change an image using JavaScript, the image fails to load

I am having trouble understanding what is wrong with my code. <html> <body> <script type="text/javascript"> function changeImage() { var image = document.getElementById('myImage'); if (image.src.match("bulbon")) { ...

Ways to incorporate the setTimeOut function

<body> <script> $(window).scroll(function() { $('#csgo').each(function(){ var imagePos = $(this).offset().top; var topOfWindow = $(window).scroll ...

How can I generate an AJAX object to handle multiple requests or as an array?

I am looking to dynamically create Ajax objects for each request and execute a function for each object using jQuery. Here is an example in JavaScript: xhr[rand] = new XMLHttpRequest(); xhr[rand].open("post", "base64upload", true); xhr[rand].upload.add ...

Learn the steps for inserting or updating data in a local JSON file solely through JavaScript

Currently, I am in the process of reading a JSON file and removing an element once it finds an exact match. My goal is to then push the remaining data back into the JSON file after deletion. readJsonFile("./objects.json", function(jsonData){ let parsedD ...

Enhancing Material UI List Items with Custom Styling in React.js

My website's navigation bar is created using material-ui components, specifically the ListItem component. To style each item when selected, I added the following CSS code: <List> {routes.map(route => ( <Link to={route.path} key={ro ...

:after pseudo class not functioning properly when included in stylesheet and imported into React

I am currently utilizing style-loader and css-loader for importing stylesheets in a react project: require('../css/gallery/style.css'); Everything in the stylesheet is working smoothly, except for one specific rule: .grid::after { content: ...

Modify the AJAX data in Datatables without directly modifying the elements

I am currently working with a Datatable that is being populated through AJAX, and everything is going smoothly. However, I am looking for a way to include some shortcuts to send requests to the server. The issue lies in how I can modify the data being sent ...

PHP AJAX 12017 Issue with Header(Location:) Redirection

My problem involves a jQuery function that utilizes AJAX to call a PHP file. In the PHP file, I have included the following code snippet: header('Location: http://www.google.com'); Unfortunately, this method does not successfully redirect the ...

Displaying data on the user interface in Angular by populating it with information from the form inputs

I am working on a project where I need to display data on the screen based on user input received via radio buttons, and apply specific conditions. Additionally, I require assistance in retrieving the id of an object when the name attribute is chosen from ...

Ways to customize the appearance of a specific column in a k-grid td

While working with a kendo grid, I encountered an issue with setting a tooltip for one of the columns. After some experimentation, I discovered that in order for the tooltip to display correctly, I needed to override the overflow property on the k-grid td ...

Why is DRF interpreting my jQuery Ajax request as arrays?

Encountering an issue during the following process: Trying to make a $.ajax call: $.ajax({ type: "POST", url: '/endpoint', headers: {"X-CSRFToken": csrf_token}, data: { 'action': 'my-action', 'data&ap ...

Is there a way to ensure that my slideshow images maintain their proportions when viewed on various screen sizes?

I came across a code snippet for an image slideshow that I really liked, but it didn't resize properly on different browser sizes. Initially, I attempted to use the vh property to address this issue, but unfortunately, the images wouldn't scale p ...

One login for accessing multiple forms

I am trying to figure out a way to use one login for two different forms that serve different functions. How can I pass the login details between these two functions? Just to clarify, I only have knowledge of JavaScript and VBScript, not jQuery. For inst ...