Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle a class in Blazor on button click?

I've currently got an onclick event that toggles a class like so:

....
<button id="@m.TargetCo.ButtonId" onclick="glyphChanger(this.id)" class="btn btn-default iconButton glyphicon glyphicon-chevron-right" ></button>
....
function glyphChanger(buttonID) {
    $("#" + buttonID).toggleClass('glyphicon-chevron-right glyphicon-chevron-up');
}
like image 368
Bhav Avatar asked Dec 22 '25 20:12

Bhav


1 Answers

No need javascript. You can do this the Blazor way.

DropDownComponent.razor

<h3>Debug: @buttonCss</h3>
<button type="button" class="btn btn-default iconButton glyphicon @buttonCss" @onclick="ChangeButtonClass">Test</button>

@code {
    private string buttonCss = "glyphicon-chevron-right";
    private void ChangeButtonClass()
    {
        buttonCss = buttonCss == "glyphicon-chevron-right" ? "glyphicon-chevron-up" : "glyphicon-chevron-right";
    }
}

Somewhere else in the application

<DropDownComponent/>
<DropDownComponent/>
<DropDownComponent/>
<DropDownComponent/>
like image 183
clamchoda Avatar answered Dec 24 '25 09:12

clamchoda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!