Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Can I make a button's text change dynamically on click?

Stewart Eade July 21, 2022

I have stolen a user macro for "Expand All" from this forum, but my users have asked if it is possible to make the text in the button change based on the state of the page.

So it toggles between "Expand" or "Collapse" based on whether the button has been clicked.

This is the macro I have used, but not sure how to change to get dynamic button name:

## Macro title: Expand All
## Macro has a body: N
## Developed by: George Lewe
## Date created: 2014-09-22
## Installed by: George Lewe
## @noparams
<p align="left"><button name="toggleAll" id="toggleAll" type="button" class="aui-button" aria-pressed="false" style="background-color:blue;color:white;font-weight:bold">Expand All / Collapse All</button></p>

<script type="text/javascript">
AJS.toInit(function ()
{
AJS.$('#toggleAll').click(function()
{
var expanded = jQuery(".expand-icon.aui-iconfont-chevron-down").closest("div");
var unexpanded = jQuery(".expand-icon.aui-iconfont-chevron-right").closest("div");
if (expanded.size() > unexpanded.size()) {
expanded.each(function(){ jQuery(this).trigger("click");})
} else {
unexpanded.each(function(){ jQuery(this).trigger("click");})
}
});
});
</script>

1 answer

0 votes
Brittany Joiner
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 21, 2022

@Stewart Eade which button in trello are you trying to change? i could probably make something with PixieBrix that changes the text of the button when its clicked, but to show an example I'd need to know which button you're talking about

Stewart Eade July 22, 2022

Thanks for your response. I managed to work it out and the new code (shown in BOLD) below:-

## Macro title: Expand All
## Macro has a body: N
## Developed by: George Lewe
## Date created: 2014-09-22
## Installed by: George Lewe
## @noparams
<p align="left"><button name="toggleAll" id="toggleAll" type="button" class="aui-button" aria-pressed="false" style="background-color:blue;color:white;font-weight:bold">Expand All</button></p>

<script type="text/javascript">
const btn = document.getElementById("toggleAll");

AJS.toInit(function ()
{
AJS.$('#toggleAll').click(function()
{
var expanded = jQuery(".expand-icon.aui-iconfont-chevron-down").closest("div");
var unexpanded = jQuery(".expand-icon.aui-iconfont-chevron-right").closest("div");
if (expanded.size() > unexpanded.size()) {
expanded.each(function(){ jQuery(this).trigger("click");
btn.innerText = "Expand All";})
} else {
unexpanded.each(function(){ jQuery(this).trigger("click");
btn.innerText = "Collapse All";})
}
});
});
</script>

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events