django_bootstrap_icons: change icon on click

187 views
Skip to first unread message

Arno Schuurs

unread,
Jul 15, 2021, 9:35:36 AM7/15/21
to Django users

Hello,

After our intern left, I took over his code.
I am wondering if it is possible to change the button icon after clicking it.

Example:
expand, use a 'chevron-down'
collapse , use a 'chevron-up'

The code that I have:
{% load django_bootstrap_icons %}
...
<button class="btn btn-sm p-1
                                       {% if lane.type == 'PRE' %}btn-outline-dark {%else %} btn-outline-light {% endif %}
                                      small block-collapsebtn"
                                              type="button"
                                              id="CollapseBtn"
                                              data-toggle="collapse"
                                              data-target="#collapsable-{{block.uuid}}"
                                              aria-expanded="false"
                                              aria-controls="collapseExample"
                                              onclick="change_btn(this)"
                                      >{% bs_icon 'chevron-down' %}</button>

Could you help out?
Thank you.

Best Regards
Arno Schuurs

Fab

unread,
Jul 15, 2021, 2:56:50 PM7/15/21
to Django users
Hi Arno,

You will have to change the buttons value (its text) with Javascript.

In the change_bnt() function you can change the button's value with:
document.getElementById("CollapseBtn").value="something"; 

You could toggle the value depending on what the value already is:
if (value == 'a') value = 'b'
else value = 'a'

You could pass the chevron values into the Javascript by button the value in a var on the template:
var chevronDown = {% bs_icon 'chevron-down' %};

Hope this helps.

Fab

Arno Schuurs

unread,
Jul 16, 2021, 3:36:07 AM7/16/21
to Django users
I have this working, but not yet with {% bs_icon 'chevron-down' %} and {% bs_icon 'chevron-down' %}

                                       <script>
                                         function change_btn(objButton)
                                         {
                                           var btnval = objButton.innerHTML;
                                           var btnUp = "up";
                                           var btnDown = "down";
                                           if (btnval==btnDown) objButton.innerHTML = btnUp;
                                           else objButton.innerHTML = btnDown;
                                         }
                                       </script>

                                       <button class="btn btn-sm p-1
                                       {% if lane.type == 'PRE' %}btn-outline-dark {%else %} btn-outline-light {% endif %}
                                      small tasblock-collapsebtn"

                                              type="button"
                                              id="CollapseBtn"
                                              data-toggle="collapse"
                                              data-target="#collapsable-{{tasblock.uuid}}"

                                              aria-expanded="false"
                                              aria-controls="collapseExample"
                                              onclick="change_btn(this)"
                                      >down</button>

Op donderdag 15 juli 2021 om 20:56:50 UTC+2 schreef Fab:

Arno Schuurs

unread,
Jul 16, 2021, 3:55:51 AM7/16/21
to Django users
This seems to work.
                                       <script>
                                         function change_btn(objButton)
                                         {
                                           var btnval = objButton.innerHTML;
                                           var btnUp = '{% bs_icon "chevron-up" %}';
                                           var btnDown = '{% bs_icon "chevron-down" %}';

                                           if (btnval==btnDown) objButton.innerHTML = btnUp;
                                           else objButton.innerHTML = btnDown;
                                         }
                                       </script>
                                       <button class="btn btn-sm p-1
                                       {% if lane.type == 'PRE' %}btn-outline-dark {%else %} btn-outline-light {% endif %}
                                      small tasblock-collapsebtn"

                                              type="button"
                                              id="CollapseBtn"
                                              data-toggle="collapse"
                                              data-target="#collapsable-{{tasblock.uuid}}"

                                              aria-expanded="false"
                                              aria-controls="collapseExample"
                                              onclick="change_btn(this)"
                                      >{% bs_icon "chevron-down" %}</button>
Op donderdag 15 juli 2021 om 20:56:50 UTC+2 schreef Fab:
Hi Arno,
Message has been deleted

Fab

unread,
Jul 16, 2021, 3:57:57 AM7/16/21
to Django users
Yea that's how I'd handle it.
Reply all
Reply to author
Forward
0 new messages