Elements that return to original size when clicked again or when a brother element is clicked

Asked

Viewed 47 times

-1

I have a table with some <td> align vertically. When clicked, the height of the <td> increases, and if another is clicked <td>, the previous one back to its original size. I also need the element to return to its original size when clicked again.

So far, I’ve only managed to change the height in the first click:

$('#tabela_funcoes td').click(function() {
  $(this).animate({
    height: '300px'
  }, "slow" )
})
  • A toggleClass wouldn’t it? [http://api.jquery.com/toggleclass/]

1 answer

0


The click event is on the line (<tr>) and you can catch all siblings of the clicked element to restore height. Then increase the height of the clicked element. Bootply with the test: https://www.bootply.com/FAoXyzLzGd

$('#teste tbody tr').on('click', function() {
  $(this)
    .siblings()
    .animate({
      height: '50px'
    });

  $(this).animate({
    height: '150px'
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="teste" class="table table-responsive">
  <thead>
    <tr>
      <th>Coluna 1</th>
      <th>Coluna 2</th>
      <th>Coluna 3</th>
    </tr>
  </thead>
  <tbody>
    <tr style="height: 50px">
      <td>Valor 1</td>
      <td>Valor 2</td>
      <td>Valor 3</td>
    </tr>
    <tr style="height: 50px">
      <td>Valor 1</td>
      <td>Valor 2</td>
      <td>Valor 3</td>
    </tr>
    <tr style="height: 50px">
      <td>Valor 1</td>
      <td>Valor 2</td>
      <td>Valor 3</td>
    </tr>
    <tr style="height: 50px">
      <td>Valor 1</td>
      <td>Valor 2</td>
      <td>Valor 3</td>
    </tr>
    <tr style="height: 50px">
      <td>Valor 1</td>
      <td>Valor 2</td>
      <td>Valor 3</td>
    </tr>
  </tbody>
</table>

  • Thanks!! When I clicked on the td, there was a delay before the block increased in size. Can you explain why? By the way: is there any way for me to control the side to which the element expands? Because when I click on the bottom, the top also moves by pushing the top element, which bothers a little visually

  • Ah! And the size of the block does not return to normal when clicked again. I need to use if, as in the other colleague’s answer?

  • I upgraded the bootply to catch the initial height. When the document is loaded, the code creates a variable called 'initialHeight' with the initial height of the lines, and is used in the future to return to the original height.

  • About the direction of expansion, I don’t believe it exists. If it exists, I don’t know. What happens is that they are vertically aligned elements. If you increase the size, it is necessary to push the bottom down. When it decreases, naturally you pull them up (in case, the lower ones will stand still, because another will increase the size and keep them in the same position). I tested with the second expanded line and expanded the third in the sequence. The bass lines become static.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.