Custom Fields
Re-Envisioned
Support General Dropdown disable option

  • Creator
    Topic
  • #13843
    woorise
    Participant

    I use two select_advanced fields and when I select an option in the first dropdown I want to disabled it from the second dropdown.

    I use the following code. The disabled attribute added successfully but the option isn’t disabled.

    
    (function($) {
    
        $('#_language_pairs_from').on('change', function() {
            var selectVal = $(this).val();
            $('#_language_pairs_to option[value="'+selectVal+'"]').prop('disabled', true);
        });
    
    })( jQuery );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #13867
    Anh Tran
    Keymaster

    Hi,

    I think you need to use select2 API. Your code seems to work only for normal select.

    #13987
    woorise
    Participant

    I am using the exact same code as in the jsfiddle http://jsfiddle.net/u3tq89wg/ in a clean installation but it works only the first time. It doesn’t updated after I open the second dropdown. I don’t get any console errors.

    Can you please give me a working example?

    #13999
    Anh Tran
    Keymaster

    I made a modification to that script, here is a working version: https://ghostbin.com/paste/tqqof

    #14013
    woorise
    Participant

    Thank you very much! Now it works!

    I notice that we destroy the select and initialize it again. Doing this we lose the initial options. Is there any way to automatically set the initial options?

    #14020
    Anh Tran
    Keymaster

    Ah, when initialize the select2, you can get options via data-options and pass it to the initializer:

    var options = $s2.data( 'options' );
    $s2.select2( options );
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.