I have the following HTML:
<select class="select optional" data-minimumresultsforsearch="10"
data-query="function (query) {var data = {results: []}, i, j, s; for (i = 1; i < 5; i++) {s = ""; for (j = 0; j < i; j++) {s = s + query.term;} data.results.push({id: query.term + i, text: s});} query.callback(data);}"
id="user_default_locale" name="user[default_locale]"></select>
I need to find a javascript function that loops over all the elements like select. That’s the part that I found myself 🙂
document.getElementsByTagName('select')
But now I need to call them all like this:
$("user_default_locale").select2({
minimumresultsforsearch: 10,
query: function (query) {
var data = {results: []}, i, j, s;
for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) {s = s + query.term;}
data.results.push({id: query.term + i, text: s});
}
query.callback(data);
}
});
Anyone know how to do this?
EDIT:
Ok I now have this:
$('select').each(function(idx, elem) {
var $elem = $(elem);
var VarMinimumResultsForSearch = $elem.data('minimumResultsForSearch');
var VarPlaceholder = $elem.data('placeholder');
$elem.select2({minimumResultsForSearch: VarMinimumResultsForSearch || undefined, placeholder: VarPlaceholder || undefined});
});
And with this it does execute the select2 however not with the params…
<select class="select optional" data-minimumresultsforsearch="1" data-placeholder="JOS"
id="user_default_locale" name="user[default_locale]">
If you are going to loop over selects, here is what you might want to try: