Setting select2 input value with jquery

I am trying to set select2 value using jquery. This is probably a recurring question, but I read 30 different answers here and did not find what solves my problem.

HTML code:

<div class="has_many_fields" data-required-has-many-fields-rows="1" id="location_account_associations"><input id="location_account_associations_attributes_0__destroy" name="location[account_associations_attributes][0][_destroy]" type="hidden" value="false" />
<div class="has_many_fields_row countable" id="location_account_associations_attributes_0">
    <div class="form__row form__row--spreadsheet">
        <div class="form__row__body">
            <div class="form__row__field-wrapper">
                <ul class="grid grid--no-gutters">
                    <li class="grid__6 grid__x--show">
                        Account Thing
                        <input id="location_account_associations_attributes_0_ab_account_id" name="location[account_associations_attributes][0][ab_account_id]" type="hidden" value="42" />
                    </li>
                    <li class="grid__5">
                        <select class="js-select2-combobox" id="location_account_associations_attributes_0_account_id" name="location[account_associations_attributes][0][account_id]" placeholder=" " reflection="#&lt;ActiveRecord::Reflection::AssociationReflection:0x012fb5asd200e8&gt;">
                            <option value=""></option>
                            <option value="1">Orange</option>
                            <option value="2">Apple</option>
                        </select>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

I tried several different approaches to select Orange. Note that this jQuery is executed when a certain key press is pressed. In addition, for a number of reasons, HTML is not editable. Only jQuery can be changed.

Javascript / jQuery:

(function() {
    $(document).on("keydown", function(e) {

        // ignore unless CTRL + ALT/COMMAND + N was pressed
        if (!(e.keyCode == 78 && e.ctrlKey && e.altKey )) return

        jQuery('[name*="[account_associations_attributes][0]').select2("val",1);
        jQuery('[name*="[account_associations_attributes][0]').select2("val","Orange");
     })
 }())
+4
source share
3 answers

Try the following:
You can select it by selecting id.
Demo version: http://jsfiddle.net/5Y9mG/10/

  $(document).ready(function(){

    $(document).on("keydown", function(e) {

        // ignore unless CTRL + ALT/COMMAND + N was pressed
     if (!(e.keyCode == 78 && e.ctrlKey && e.altKey )) return;
        $('#location_account_associations_attributes_0_account_id option:eq(1)').prop('selected', true)
     });
});

:

 $('#location_account_associations_attributes_0_account_id option[value="1"]').prop('selected', true)
+2

.

jQuery('[name*="[account_associations_attributes][0]"]').select2('val', '1')
+7

Detecting a few keystrokes, especially with modifiers, is very difficult.

Check out this other post and then make sure your keypress event generates a simpler result (something simple, for example alert()) before adding more complex behavior, such as changing the values ​​of a drop-down menu, which can also be complex depending on the browser and the drop-down list itself.

-1
source

All Articles