Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable/disable jQuery UI comboboxes?

I am using jquery UI. I have 3 comboboxes and I need to enable each field if the previous one has been filled.

The following is my current code:

 jQuery(document).ready(function(){  
        $("#box1").combobox();  
        $("#box2").combobox();  
        $("#box3").combobox();  
 }); 
like image 871
Akhmed Avatar asked Oct 21 '10 16:10

Akhmed


1 Answers

you can set up your event listener for "selected" when you init your comboboxes, set a default "state" of disabled on your second and third ones. An easy way would be to wrap your select lists in a span or div with similar id so you can target the generated INPUT and BUTTON items created by jQuery UI.

You didn't provide much context / code to go on, so I created a working example HERE that might serve to get you started.

JS:

      // init autocomplete combobox 1 with event handler
      $("#box1").combobox({
         selected: function(event, ui) {
            // now that we have an event listener, we can do whatever we like on the event.
            $("#test2 input").removeAttr('disabled');
            $("#test2 button").removeAttr('disabled');
         }
      });

      // init autocomplete combobox 2 with event handler
      $("#box2").combobox({
         selected: function(event, ui) {
             // now that we have an event listener, we can do whatever we like on the event.
            $("#test3 input").removeAttr('disabled');
            $("#test3 button").removeAttr('disabled');
         }
      });    

    $("#box3").combobox(); // init autocomplete combobox 3

    // set initial state of generated combobox 2
    $("#test2 input").attr('disabled',true);
    $("#test2 button").attr('disabled',true);  

    // set initial state of generated combobox 3
    $("#test3 input").attr('disabled',true);
    $("#test3 button").attr('disabled',true);  

HTML:

<div class="demo">
  <div class="ui-widget">
    <div id="test1">
      <label>Box 1: </label>
      <select id="box1">
        <option value="">Select one...</option>
        <option value="ActionScript">ActionScript</option>
        <option value="AppleScript">AppleScript</option>
        <option value="Asp">Asp</option>
        <option value="BASIC">BASIC</option>
        <option value="C">C</option>
        <option value="C++">C++</option>
        <option value="Clojure">Clojure</option>
        <option value="COBOL">COBOL</option>
        <option value="ColdFusion">ColdFusion</option>
        <option value="Erlang">Erlang</option>
        <option value="Fortran">Fortran</option>
        <option value="Groovy">Groovy</option>
        <option value="Haskell">Haskell</option>
      </select>
    </div>
    <div id="test2">
      <label>Box 2: </label>
      <select id="box2">
        <option value="">Select one...</option>
        <option value="Java">Java</option>
        <option value="JavaScript">JavaScript</option>
        <option value="Lisp">Lisp</option>
        <option value="Perl">Perl</option>
        <option value="PHP">PHP</option>
        <option value="Python">Python</option>
        <option value="Ruby">Ruby</option>
        <option value="Scala">Scala</option>
        <option value="Scheme">Scheme</option>
      </select>
    </div>
    <div id="test3">
      <label>Box 3: </label>
      <select id="box3">
        <option value="">Select one...</option>
        <option value="BASIC">BASIC</option>
        <option value="C">C</option>
        <option value="C++">C++</option>
        <option value="Clojure">Clojure</option>
        <option value="COBOL">COBOL</option>
        <option value="ColdFusion">ColdFusion</option>
      </select>
    </div>    
  </div>
</div>
like image 164
Betard Fooser Avatar answered Sep 22 '22 13:09

Betard Fooser