skip to Main Content

Is there a way to make an input visible based on a bool variable?

<input type="button" value="Smth" onclick="...">

I am trying to find an HTML helper for this purpose, however no luck so far.

2

Answers


  1. try this

    @if ( your bool condition)
    {
    <input type="button"   value="Smth" onclick="..."> //visible
    }
    else
    {
    <input type="button" class="d-none" value="Smth" onclick="..."> //none-visible
     
    //or
    
    <input type="button" hidden value="Smth" onclick="...">
    }
    
    Login or Signup to reply.
  2. You can achieve your required result to set your input field as disabled by using the ternary conditional operator:

    <input type="button" value="Smth" onclick="..." @(myBoolCondition == "true" ? "disabled='disabled'" : "") >
    

    Where myBoolCondition is your variable holding the result of your condition.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search