Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Deactivate an Activated Button


Hi, I am using metronic theme with blazor webapp dotnet 9. I have this code:

@foreach (var parking in Parkings ??= [])
{
<button type="button"
class="btn btn-lg btn-light-primary @(parking.IsActive ? "active" : "") w-100 mb-3"
@onclick="() => { parking.IsActive = !parking.IsActive; }">
@parking.Name
</button>
}

the problem is that when I click on the button, It gets active class and when I click again, the active class will remove. but after the removal of active class, the button style remains active (Full Primary Blue) until i click anywhere else on the screen.

I tried
1- jquery addClass and removeClass
2- bootstrap :

const button = bootstrap.Button.getOrCreateInstance(buttonElement)
button.toggle()

3- use data-bs-toggle of bootstrap
4- calling StateHasChanged();

none of them worked. button stays in active style until click somewhere else. I mean for example this button has btn-light-primary class and as you know, It is by default, a light blue button but after the active class added to element, It is like a button with class btn-primary and it stays in that color or style format until I click on screen so It knows to render the button and show the correct style


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)

Deleted comment

Hi,

Thank you for reaching out to us.

This is an expected behavior, clicking button activates button active state and active state is selected in styles to override button default color.

.primary:active:not(.btn-active) {
color: var(--bs-primary-inverse);
border-color: var(--bs-primary-active);
background-color: var(--bs-primary-active)
}

If you want manually handle button active state you can use btn-active class.

Regards,
Lauris Stepanovs,
Keenthemes Support Team



thank you,
as you said, I changed my code to manually add and remove btn-active class but now it doesn't even activate the active style!
current code:

<button type="button"
class="btn btn-lg btn-light-primary @(parking.IsActive ? "btn-active" : "") w-100 mb-3"
@onclick="() => ToggleParking(parking)">
@parking.Name
</button>

here is the video to see new code effect (btn-active class):

video


and here is the effect of active class:

video2



Hi Ali,

Other solution you can try is manually remove the active state in your button click handler.

JS.InvokeVoidAsync("eval", "document.activeElement?.blur()");


Regards,
Lauris Stepanovs,
Keenthemes Support Team


Deleted comment
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(