Home  >  Q&A  >  body text

How to switch classes in Blazor on button click?

I currently have an onclick event that switches classes like this:

....
<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');
}

P粉041881924P粉041881924205 days ago545

reply all(1)I'll reply

  • P粉269847997

    P粉2698479972024-03-28 00:03:54

    No JavaScript required. You can do this the Blazor way.

    DropDownComponent.razor

    Debug: @buttonCss

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

    Elsewhere in the app

    
    
    
    

    reply
    0
  • Cancelreply