The size of table cells unexpectedly shifts following a click event

As I construct a table dynamically using JS, I encounter an issue where clicking on the btn_dropdown0 button causes it to shift slightly to the left and right. This anomaly occurs because the two empty th elements are resizing when the tbody is toggled to be visible or hidden. These blank cells seem to stretch whenever the row below them contains data.
This is my replicated table along with the jQuery code to handle the effects:

$('#btn_dropdown0').click(function() {
if($(this).css('transform') == 'matrix(-1, 0, 0, -1, 0, 0)'){
$(this).css({'transform': ''});
$('#tbody0').hide();
}else{
$(this).css({'transform': 'rotate(180deg)'});
$('#tbody0').show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<table class="table table-hover text-center">
        <thead class="thead-dark">
            <tr>
                <th scope="col">Bangkok</th>
                <th scope="col">Milan</th>
                <th scope="col">Flight type: international</th>
                <th scope="col"></th>
                <th scope="col"></th>
                <th scope="col"><i class="fas fa-chevron-circle-down" id="btn_dropdown0"></i></th>
            </tr>
        </thead>
        <tbody id="tbody0" style="display: none;">
            <tr>
                <td>Monday</td>
                <td>15:00</td>
                <td>16:00</td>
                <td>Supported airplanes: 1,4,5</td>
                <td><button class="btn btn-info">Reserve</button></td>
            </tr>
            <tr>
                <td>Tuesday</td>
                <td>22:00</td>
                <td>23:00</td>
                <td>Supported airplanes: 1,4,5</td>
                <td><button class="btn btn-info">Reserve</button></td>
            </tr>
        </tbody>
    </table>

Answer №1

To maintain the position of th, add a width to it. Currently, without any specified width, the th element adjusts its size based on the content inside it. When you click on it, you may notice flickering due to the scrollbar. Check this behavior in full-page mode.

$('#btn_dropdown0').click(function() {
      if($(this).css('transform') == 'matrix(-1, 0, 0, -1, 0, 0)'){
        $(this).css({'transform': ''});
        $('#tbody0').hide();
      } else {
        $(this).css({'transform': 'rotate(180deg)'});
        $('#tbody0').show();
      }
    });
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<table class="table table-hover text-center">
  <thead class="thead-dark">
    <tr>
      <th scope="col" width="15%">Bangkok</th>
      <th scope="col" width="15%">Milan</th>
      <th scope="col" width="45%">Flight type: international</th>
      <th scope="col" width="10%"></th>
      <th scope="col" width="10%"></th>
      <th scope="col"><i class="fas fa-chevron-circle-down" id="btn_dropdown0"></i></th>
    </tr>
  </thead>
  <tbody id="tbody0" style="display: none;">
    <tr>
      <td>Monday</td>
      <td>15:00</td>
      <td>16:00</td>
      <td>Supported airplanes: 1,4,5</td>
      <td><button class="btn btn-info">Reserve</button></td>
    </tr>
    <tr>
      <td>Tuesday</td>
      <td>22:00</td>
      <td>23:00</td>
      <td>Supported airplanes: 1,4,5</td>
      <td><button class="btn btn-info">Reserve</button></td>
    </tr>
  </tbody>
</table>

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 ensure the execution of requests within async.each in nodejs?

I am facing an issue with my code that uses async.each to iterate through an array and execute a function called "check" for each element. The check function contains a request, but when I run the code, I find that Node.js is not executing the check functi ...

Hiding the C3 tooltip after engaging with it

I'm currently expanding my knowledge on utilizing C3.js for creating charts, and one aspect I'm focusing on is enhancing the tooltip functionality. Typically, C3 tooltips only appear when you hover over data points as demonstrated in this example ...

Can someone explain the variance between these two script codes? Are they entirely distinct, or can I utilize just one source?

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> It appears that one script is for jQuery and the ...

Creating a Website for Compatibility with NoScript

During my journey of building a nameplate site from the ground up for myself, I have delved into the realms of learning and establishing my online presence. The highlight of my project is a sleek tabbed site that employs AJAX and anchor navigation to seaml ...

Add a new key-value pair to each object in an array, with the value generated by making a request using axios.get

Recently, I've been working on a scraper that has been functioning well until I encountered an issue while trying to scrape data for individual links. Let me clarify the situation: The scraper gathers data about apartments from a webpage where articl ...

Using Redux with Next.js to implement getStaticPaths

Can someone help me understand how to implement getStaticPaths in conjunction with Redux in Next.js? I'm currently using next-redux-wrapper to manage my content, but I am encountering issues when trying to display the data. Below is a snippet of my ...

The horizontal display of images is currently experiencing issues

I am having trouble displaying images horizontally in a row. I am aiming to show 8 images next to each other, but my current grid system setup is not achieving this. The images are currently stacked on top of each other, but I want them to be aligned horiz ...

Tips for saving an image that has been dragged onto the browser locally

I recently started working with Angular and decided to use the angular-file-upload project from GitHub here. I'm currently in the process of setting up the backend for my application, but I'd like to be able to display dropped files locally in th ...

`Uncovering solutions for AJAX's undefined POST index issue`

When I try to send an ajax "POST" request in my php file, the index of the POST ends up being undefined. This is the snippet of code I am using for the ajax call: $(".add-percentage").click(function(){ var percentage_id = $(this).data('landing_ ...

Tips for incorporating a favicon in a React application

Having trouble adding a favicon to my React application. I followed the instructions in this post, but it's not working for me. I placed the favicon.ico file inside the public folder where index.html resides. This is how my directory structure looks ...

`Is it necessary to handle textStatus when encountering an HTTP error during an AJAX request?`

When utilizing jQuery and encountering an AJAX request failure attributed to an HTTP error (e.g., 500 Internal Server Error), what exactly is the assigned value of the textStatus parameter within the error handler function? For instance, $.ajax(...).fail( ...

Learn the step-by-step process of converting JSON functionality into HTML/text output using Golang

I need to modify the API responses to display as HTML or text, as the current script only outputs JSON format. func writeJSONResponse(w http.ResponseWriter, statusCode int, data interface{}) { w.Header().Set("Content-Type", "applicatio ...

Exploring the implementation of the jquery .counterUp function within Angular 5

I am working on implementing a counter up view for my company's website using the jQuery counterUp function. Despite adding script tags for it, Angular is not recognizing the function. if(st >= hT ){ $('.counter').counterUp({ dela ...

Enhance the efficiency of traversing a lengthy list of comparisons within an array.filter method

I am struggling with finding an efficient method to filter out items from an array of objects based on two specific attributes matching those of the last 6 items of another array. Currently, I have a manual process in place which results in cumbersome and ...

Display an additional div when hovering over form input

I am currently in the process of designing a search bar animation. Specifically, I want the search history to appear when hovering over the search bar. However, I am facing an issue where the code doesn't seem to work when I use .search-bar-input:hove ...

The Bootstrap 4 Carousel experiences flickering issues during transitions when viewed on Safari browser

After integrating a Bootstrap 4 carousel onto a Drupal platform, I encountered a peculiar issue. The carousel functions smoothly on Chrome, but on Safari, it briefly blinks before transitioning to the next slide (as per the set interval time). Initially, I ...

Here's a tip on how to trigger a function when the text in an input box changes using JavaScript

While using Vue, I am developing a form which includes an input box. Whenever the text in the input box changes, it should update the function setMessage. <input type="text" v-model="test" v-on:change"setMessage"> However, the issue is that the upd ...

Encountering a parse error when making an AJAX call using structural functions

I'm in the process of developing an API and here is my PHP function. function retrieve_schools($cn){ $schools_query = "SELECT * FROM schools"; $school_result = mysqli_query($cn, $schools_query); $response_array['form_data'][&apo ...

Utilize Ramda to selectively filter each inner property within arrays

I have a data structure that looks like this: const arrays = { one: [[1, 33, 41], [2, 0, 27], [3, 7, 9], [4, 1, 3]], two: [[1, 77, 2], [2, 6, 3], [3, 0, 0], [4, 55, 3]], three: [[1, 4, 6], [2, 0, 0], [3, 5, 6], [4, 0, 0]], }; In this structure, the ...

Attention: It is not possible to update the React state on a component that is not mounted. Please use the useEffect cleanup function

Can you assist with solving this issue? https://i.sstatic.net/yKFzr.png //ReviewProductDetail.jsx : Initially, the page was populated from this particular component import React, { useEffect, useState } from 'react'; import RatingCardProductD ...