Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally set the disabled state of a bootstrap button

I have the following button in my view:

<button type="button" class="btn btn-lg btn-primary">Primary button</button>

I want to add the disabled property to it if and only a certain condition is true. How do I this in an elegant way?

like image 518
user1175969 Avatar asked Oct 22 '25 05:10

user1175969


2 Answers

You could use the rails helper button_tag with a boolean check

<%= button_tag "Primary button", class: 'btn btn-lg btn-primary', 
  disabled: condition_check_method %>
like image 108
Ropeney Avatar answered Oct 24 '25 20:10

Ropeney


You Should do this two or more way:

<button type="button" class="your-class btn btn-lg btn-primary">Primary button</button>

<script type="text/javascript">
  $(function(){
    if(Condition){
      $('.your-class').addClass('disabled');
    }
  });
</script>

OR
In The rails term 

<button type="button" class="btn btn-lg btn-primary <%= conditon ? "disabled" : ""%>">Primary button</button>
like image 34
Kaushlendra Tomar Avatar answered Oct 24 '25 19:10

Kaushlendra Tomar