2

How can I Toggle a Selectbox between Enable and Disable onClick button...

Scenario: If I click on #locked div, this div should toggle with another class "icon-unclocked" and selectbox should be enabled. If I click on "#locked" again, selectbox should be disabled and "icon-unclocked" has to be removed.

First part (toggleClass) is working great.. but having problem in enable / disable selectbox...

FIDDLE

Please check the code below...

HTML:

<div class="icon-locked" id="locked">&nbsp;</div>
<select class="form-control" id="signontype" disabled>
  <option>Option 01</option>
  <option>Option 02</option>
</select>

jQuery:

$(document).ready(function(){
    $( "#locked" ).click(function() {
      $( this ).toggleClass( "icon-unlocked" );
      $('select#signontype').prop('disabled', false);
    });
});
Reddy
  • 1,477
  • 29
  • 79

4 Answers4

4

Try this:

$("#locked").click(function () {
    $(this).toggleClass("pulse-icon-unlocked");
    $('select#signontype').prop('disabled', !$(this).hasClass("pulse-icon-unlocked"));
});

You can check if the #locked has class pulse-icon-unlocked

Demo: http://jsfiddle.net/tusharj/thhxcmso/4/

Tushar
  • 85,780
  • 21
  • 159
  • 179
3

You just need to check if your locked div contains pulse-icon-unlocked class with .hasClass()

$( "#locked" ).click(function() {
    $( this ).toggleClass( "pulse-icon-unlocked" );
    $('select#signontype').prop('disabled', !$(this).hasClass("pulse-icon-unlocked"));
});

Fiddle Example

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
3

You can use

$('select#signontype').is(':enabled')

to check whether the selectbox is enabled or not. Based on it you can do what you want.

Fiddle

logan Sarav
  • 781
  • 6
  • 27
0

$("YourElementID").selectbox('disable') will disable the selectpicker selectbox.

Note: This works fine for selectbox version 0.2. I am not sure about latest versions.

Luca Detomi
  • 5,564
  • 7
  • 52
  • 77