2

I made a dropdown list using bootstrap.Dynamically I want to disable and enable the dropdown list.

html code:

  <div class="span3 offset1">
        <select name="primary" class="select-block" id="select_alert" style="display: none;">
            <option "selected"="">Choose Alert T</option>                         


    <option value="T1">T1</option>

    <option value="T2">T2</option>

    <option value="T3">T3</option>

    <option value="T4">T4</option>

</select>
<div class="btn-group select select-block">
  <i class="dropdown-arrow dropdown-arrow-primary"></i>
  <button class="btn dropdown-toggle clearfix btn-primary" data-toggle="dropdown" id="select_alert">
    <span class="filter-option pull-left">Choose Alert T</span>&nbsp;
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu dropdown-primary" role="menu">
    <li rel="0" class="selected">
      <a tabindex="-1" href="#" class="">
        <span class="pull-left">Choose Alert T</span>
      </a>
    </li>
    <li rel="1">
      <a tabindex="-1" href="#" class="">
        <span class="pull-left">T1</span>
      </a>
    </li>
    <li rel="2">
      <a tabindex="-1" href="#" class="">
        <span class="pull-left">T2</span></a>
    </li>
    <li rel="3">
      <a tabindex="-1" href="#" class="">
        <span class="pull-left">T3</span>
      </a>
    </li>
    <li rel="4">
      <a tabindex="-1" href="#" class="">
        <span class="pull-left">T4</span>
      </a>
    </li>
  </ul>
  </div>
</div>

some times I want to disable and sometimes I want to enable.So How can I do this. In this I have 2 more dropdown elemnts.All are placed in singlw div tag.Here I didn't mention that div tag id name.I just putted single dropdown element code.

1
  • Add a disabled class to the button. duplicate of this Commented Oct 25, 2013 at 12:15

4 Answers 4

8

Yes, by jquery you can get this:

Here is the example:

http://jsfiddle.net/jV7ye/

$(document).ready(function() {
    $("#chkdwn2").click(function() {
       if ($(this).is(":checked")) { 
          $("#dropdown").prop("disabled", true);
       } else {
          $("#dropdown").prop("disabled", false);  
       }
    });
});

UPDATED JS CODE as per your need:

$(document).ready(function() {

        if(!$("#chkdwn2").is(":checked"))
        {
            $("#dropdown").prop("disabled",true);
        }

        $("#chkdwn2").click(function() {
           if ($(this).is(":checked")) { 
              $("#dropdown").prop("disabled", false);
           } else {
              $("#dropdown").prop("disabled", true);  
           }
        });
    });

Replace dropdown id with your id. Thanks

Sign up to request clarification or add additional context in comments.

3 Comments

I your jsFiddle code,interchange trur->false and false->true.That means Initially I want to disabled.it's not working.
Have you clicked on "Run" after interchanging true and false?
Please paste this code in javascript and try again: $(document).ready(function() { if(!$("#chkdwn2").is(":checked")) { $("#dropdown").prop("disabled",true); } $("#chkdwn2").click(function() { if ($(this).is(":checked")) { $("#dropdown").prop("disabled", false); } else { $("#dropdown").prop("disabled", true); } }); });
3

By using jquery we can do

Disable Dropdown

$('.select_alert').attr('data-toggle','');

Enable Dropdown

$('.select_alert').attr('data-toggle','dropdown');

3 Comments

Initially I want to dropdowns after than enable.I tried your code in web-console page.it's working fine.that same code I pasted in my .js file.here it's not working.
you dont need to paste on js file.write seperate function to enable and disable and call that function sure it work
I made a function, Initially I passed data-toggle value is null("") to that function.If I call function I am getting the following error. Uncaught TypeError: Cannot call method 'setAttribute' of undefined .
0

If above solutions does not work for you, the try

$('#yourDropDownElement').prop('disabled', true).trigger('chosen:updated');

I'm using boostrap v 3.3.6

Comments

-2

You can do this by add if block on the property and add disabled class to the drop down.

below is my div in that i have eanbled/disabled dropdown toggle button on IsNewEditDisabled

<button ng-if="!IsNewEditDisabled" class="btn dropdown-toggle" ng-class="{open: status.isopen}" dropdown-toggle>
                                <i class="fa fa-pencil"></i><span class="caret"></span>
                            </button>
                            <button ng-if="IsNewEditDisabled" class="btn dropdown-toggle disabled" ng-class="{open: status.isopen}" dropdown-toggle>
                                <i class="fa fa-pencil"></i><span class="caret"></span>
                            </button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.