summaryrefslogtreecommitdiff
path: root/public/assets/js/invitation.md
blob: ebcf0f7c07c689c3bfe2634d26ba120bb7262c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# SELECT2 HANDLING

handling select2 properties (case-study javascript code)


## get all options' loop through all ...

    opts = $('#sector').select2()[0].options
    for(i=0; i<opts.length; i++) { el = opts[i]; console.log(el.value, el.text); }



## set option by selected 'TEXT'

    var id;
    for(i=0; i<opts.length; i++) { if (opts[i].text == 'TEXT') id = i; }
    $('#sector').val(id).trigger('change);



## prepare as functions

    function select2_set_text(control, t) {   /* define function */
        opts = control.select2()[0].options;
        var id;
        for(i=0; i<opts.length; i++) { if (opts[i].text == t) id = i; }
        control.val(id).trigger('change');
    }
    select2_set_text( $('#sector') , '04 Physics')   /* call */



## in multiple select2 ...

    function get_id_of_text( control , t) {    /* define id from text */
        opts = control.select2()[0].options;
        var id = false;
        for(i=0; i<opts.length; i++) {
            if (opts[i].text == t) id = i;
        }
        return id;
    }
    function select2_set_multi_text(control, t_list) {   /* define function */
        var ids = [];
        t_list.forEach (t => { let id = get_id_of_text(control, t);
                if (id !== false) ids.push(id);
        })
        control.val(ids).change();
    }
    select2_set_multi_text( $('#tags') , ['crime', '90s'])  /* call */