Toggle button - show / hide information

Viewed 327

Can someone help me hide / show the information when I click on the icon (arrow). I tried to use javascript, although it is not the best way, but it also didn't work.

I intend that by clicking on the image https://img.icons8.com/android/24/000000/down.png, the information below will be hidden and that image will change to the image of the upward facing arrow https://img.icons8.com/android/24/000000/up.png

When you click on the up-facing sta the information appears again and that same image is replaced by the downward-facing arrow image.

Can someone help me?

DEMO - STACKBLITZ

CODE

<div *ngFor="let item of data">
    <div class="d-flex flex-row"
        style="align-items: center;margin-top: 8px;padding: 16px;background: #E8EEF5 0% 0% no-repeat padding-box;border-radius: 8px;">
    <div>
        <img src="https://img.icons8.com/android/24/000000/down.png" class="hide"/>
        <img src="https://img.icons8.com/android/24/000000/up.png" class="hide1"/>
  </div>
    <div><span style="margin-left: 8px;" class="selectioname">{{item.name}}</span></div>
    <div style="margin-left:auto">
        <img src="https://img.icons8.com/material-outlined/24/000000/close-window.png"/>
  </div>
    </div>
    <div class="d-flex flex-row"
                style="display: flex; align-items: center; margin-top: 8px;padding: 8px;border-bottom: 1px solid #CACED5;">
        <div class="">
            <img src="https://img.icons8.com/bubbles/50/000000/check-male.png"/>
    </div>
        <div>
            <span style="margin-left: 8px;">@{{item.name}}</span>
            <div>{{item.date}}</div>
        </div>
        <div style="margin-left:auto">
            <img src="https://img.icons8.com/material/24/000000/filled-trash.png"/>
    </div>
        </div>
</div>

enter image description here

2 Answers

First of all, remove your styles for hide1 class. It's better to rely on *ngIf directive in Angular:

<img *ngIf="item.shown" src="https://img.icons8.com/android/24/000000/down.png" class="hide"/>
<img *ngIf="!item.shown" src="https://img.icons8.com/android/24/000000/up.png" class="hide1"/>

Then implement method to toggling shown property:

toggle(item) {
  item.shown = !item.shown;
}

Now you have to bind this method to DOM event on div wrapping your arrows:

<div (click)="toggle(item)">
</div>

And final step is to use *ngIf directive on other element you want to toggling:

 <div class="d-flex flex-row" *ngIf="item.shown" ...

HTML5 introduced a <summary> and <details> tag to allow you to toggle information.

Here's an example:

<details>
<summary>Information you want shown</summary>
<p>Information you want toggled</p>
</details>

Related