pyfl88 pyfl88 - 3 months ago 6
Javascript Question

Issues of jquery in rails

I am trying to create a multiselect dropdown in a page where there are many users and dropdown box for each users. I am using jquery multiselect like this:

var handleMultiSelect = function () {
$('#my_multi_select1').multiSelect({
selectableOptgroup: true
});
$('#my_multi_select2').multiSelect({
selectableOptgroup: true
});
}


This is working but only for the first 2 multi select box. Lets say I have 100+ users in the page, how do I create separate ID for each of them? Thanks in advance.

Answer

You can simply use a class instead of an id (ids are good for unique components, classes for multiples ones):

var handleMultiSelect = function () {
    $('.multiselectable').multiSelect({
        selectableOptgroup: true
    });
}

Then in the HTML, have your element class accoringly:

<select class="multiselectable"> ... </select>

More about id vs class here: Difference between id and class in CSS and when to use it

Comments