miket-dev / multiselect

Pure js multiselect implementation
MIT License
38 stars 30 forks source link

Change events #2

Closed mamcgree closed 4 years ago

mamcgree commented 5 years ago

Greetings.. Your multi select looks great, but for the life of me I can't seem to set or capture a change event when a checkbox changes state. Could you give me an example of an onchange event so I can run a function every time an item changes.

Thank you in advance. Mark

miket-dev commented 5 years ago

It is not implemented yet. I will add this functionality with upcoming alterations.

bradjoss commented 5 years ago

I had the same issue. As a work around...

        var ms = select.multiselect();
        var items = ms._items;

        for (var i = items.length - 1; i >= 0; --i) {
            var m = items[i];
            $(m.multiselectElement).on('change', function () {
                var val = select.val();
                // My custom action here //
            });
        }

        select.on('change', function () {
            var val = $(this).val();
            // My custom action here //
        });

Thanks for a great tool, very helpful!

One more small suggestion, your each function is computing length on each iteration of the loop. If you need to process in order, you may want to cache the value outside of the loop, or go over the data backwards like above. Back in the day, this was a major performance issue for javascript on very large sets of data (thousands of iterations).

each: function (array, handler) { var len = array.length; for (var i = 0; i < len; i++) { handler(array[i]); } },

Kindest regards, Brad

miket-dev commented 4 years ago

Quite an old issue (and I am really sorry for that), but eventually I managed to add this functionality. Use setCheckBoxClick method for it. Refer to readme and demo for details.