Enhance your table design with customized styling using css and jquery

I am trying to create a webpage with an expandable and collapsible table using HTML, but I am facing some issues. Below is the code snippet I have used:

var $headers = $('.header').click(function() {
  $(this).find('span').text(function(_, value) {
    return value == '-' ? '+' : '-'
  });
  $(this).nextUntil('tr.header').slideToggle(100, function() {}); 
});
$headers.find('span').text('+')
$('table tr:not(.header)').hide()
table,
tr,
td,
th {
  border: 1px solid black;
  border-collapse: collapse;
}
tr.header {
  cursor: pointer;
}
<table border="0">
  <tr class="header">
    <th colspan="2">Header <span>-</span>
    </th>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr class="header">
    <th colspan="2">Header <span>-</span>
    </th>
  </tr>
  <tr>
    <td>date</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
  <tr>
    <td>data</td>
    <td>data</td>
  </tr>
</table>

I am experiencing difficulties as my code seems to not be functioning correctly. Can someone please help me identify the issue in my file?

Answer №1

To implement this style, you need to add the following script at the bottom of your code:

<head>
<style type="text/css">
    table, tr, td, th 
    {
    border: 1px solid black;
    border-collapse:collapse;
    }
    tr.header {
    cursor:pointer;
    }
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>
<table border="0">
    <tr class="header">
        <th colspan="2">Header <span>-</span>
        </th>
    </tr>
    <tr>
        <td>data</td>
        <td>data</td>
    </tr>
    <tr>
        <td>data</td>
        <td>data</td>
    </tr>
    <tr class="header">
        <th colspan="2">Header <span>-</span>
        </th>
    </tr>
    <tr>
        <td>date</td>
        <td>data</td>
    </tr>
    <tr>
        <td>data</td>
        <td>data</td>
    </tr>
    <tr>
        <td>data</td>
        <td>data</td>
    </tr>
</table>
<script type="text/javascript">  
     var $headers = $('.header').click(function () {
    $(this).find('span').text(function (_, value) {
        return value == '-' ? '+' : '-'
    });
    $(this).nextUntil('tr.header').slideToggle(100, function () {});
    });
    $headers.find('span').text('+')
    $('table tr:not(.header)').hide()
</script> 
<body>

Alternatively, you can place the script inside aready function:

Here's how you can do it:

<script type="text/javascript">  
$(document).ready(function(){
     var $headers = $('.header').click(function () {
    $(this).find('span').text(function (_, value) {
        return value == '-' ? '+' : '-'
    });
    $(this).nextUntil('tr.header').slideToggle(100, function () {});
    });
    $headers.find('span').text('+')
    $('table tr:not(.header)').hide()    
})

</script>  

Answer №2

Simply insert your code within $(document).ready(function({}). View the DEMO

$(document).ready(function(){ 
    /// your code here
});

To learn more about the ready() function, click HERE

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 best way to relocate a form to the top of the page

I am creating a website to enhance my HTML skills, and I encountered an issue with the sign-in form. Whenever I try to place an image next to the form, it automatically moves to the bottom. I have attempted using padding-top and padding-bottom but to no av ...

What is causing the incorrect size of <input type='range'/> in relation to content flow?

<div id='unique-div' style='background:orange; height:100%'> <input id='unique-input' type='range' style='height:100%; margin:0; padding: 0; border: 0'/> </div> <div id='another ...

Deactivate a particular function key with the help of jQuery

Is there a way to disable the F8 key on a web page using jquery, plugins, or just javascript? Thank you in advance! :) blasteralfred ...

Defining variables within a jQuery function

Within my initialization function (init), I have defined some variables and an animation that utilizes those variables. The challenge arises when I want to use the same animation/variables in my clickSlide function. http://jsfiddle.net/lollero/4WfZa/ (Un ...

The menu is not displaying the correct list style

I have created a clickable dropdown menu and I am trying to add circles behind the text on each list item (LI) but for some reason, it's not working. I checked my CSS code and I don't see any conflicts that would be causing this issue. Can someon ...

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...

Is it possible to eliminate the css class prefix when using Modular css in React?

When working with React & NextJS, I have observed that my modular SCSS files are automatically prefixing my classnames with the name of the file. Is there a way to turn off this feature as it is making the DOM structure difficult to interpret? For instanc ...

Having trouble getting smooth scrolling with easing to function properly

I'm facing an issue with a JQuery function that is supposed to enable smooth scrolling using JQuery easing, but for some reason, it's not functioning correctly and I'm unable to pinpoint the error. Here is the code for the function: $(func ...

Combining rows with identical IDs within an array of objects

Here is a representation of the array I am working with: var exampleArray = [ { "ItemID": "001", "Jan": "130", "Feb": "500" }, { "ItemID": "001", "Jan": "0&q ...

Using jQuery to reconstruct a list from a group of div elements

Seeking advice on how to convert an HTML section of divs into list items. Here is a sample of the current HTML structure: <div> <small>2019-10-31 10:41 Customer unregistered</small> </div> <div> <small>2019 ...

How can I toggle the visibility of a div based on whether a variable is defined or not?

How can I display a specific div on my webpage only when certain variables in PHP pull out a specific result from a database? I attempted to use the code snippet below, but it's not working as expected. Can someone provide guidance on how to achieve ...

Ways to prevent the submitted post from disappearing upon reloading in HTML

Currently, I am in the process of developing a website. One of my goals is to ensure that a specific post remains visible even after the page is reloaded. I want the post to persist without disappearing upon reloading. Can anyone provide guidance on how ...

"Utilizing Bootstrap to create proper spacing between elements within a

https://i.sstatic.net/ArC6C.png Hello there! I am looking for help on how to align my value 2 next to value 1 with some spacing, and also adjust the text "Lorem Ipsum" based on the size of my button. This is my current code: a <div classNa ...

Using Angular 2 to bind to a formControl in a Select2 multiple selection scenario

I have been working on implementing Select2 in my Angular2 application. While I am able to display my selection when choosing the options, I am facing an issue with binding the selected options to my Angular form control. Below is the code snippet from my ...

How come my Bootstrap 4 navbar-brand is occupying an entire row?

Struggling to create a standard navbar using Bootstrap 4? I'm facing an issue where the navbar items are not aligning properly as per the code copied from the Bootstrap website. Despite keeping the menu items short, they still move to a separate line ...

The vertical scrolling feature seems to be disabled for ng-repeat within the Angular view

I have a list of customers coming from the backend that I need to populate into ng-repeat. However, even though there are more customers, the vertical scroll is not visible. Why is that? <link rel="stylesheet" href="hike.css"> <div ng-show=' ...

Height constraint disregarded by anchor

I am facing an issue with a large textual menu where the active link space is overlapping onto the other menu items. This is causing difficulty in accurately clicking on a link. For reference, you can check out this example: http://jsfiddle.net/dhyz48j3/1 ...

Personalize product image

Currently, I am involved in a project that involves the creation of custom trading cards for clients. However, I am facing difficulty in finding a way to overlay the card template so that users can upload their photos into a specified box. Various methods ...

Jquery Gallery Snapshots

I have been utilizing this jQuery code for a SlideShow: <head> <title>Simple Slide Show with jQuery</title> <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'> < ...

Unlocking the Potential of Larger jQuery Icons

In a recent announcement, the jQuery team unveiled new icons for their UI components. However, I have been unable to locate any examples of how to use them or where they can be downloaded from. Also, it appears that the themes in the ThemeRoller only provi ...