Update CHOSEN select box with jquery and javascript
I'm using Unify template which has jquery chosen library for select boxes. My html code is as follows:
<div>
<select class="select-evaluation-makes js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required
data-placeholder="{% trans "Select brand" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-models js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Seect model" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-years js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Select year" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
</div>
And my JS is as follows:
<script>
const select_makes_selector = $(".select-evaluation-makes");
const select_models_selector = $(".select-evaluation-models");
const error_box = $("#error-box");
const error_message = $("#error-message");
$(document).ready(function () {
loadMakesOnPageLoad();
//Get models on make change
select_makes_selector.on('change', function (e, params) {
loadModels(parseInt(params["selected"]))
});
});
function loadMakesOnPageLoad() {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-makes/",
beforeSend: function () {
},
success: function (result) {
let makes = result['makes'];
select_makes_selector.append("<option></option>");
_.forEach(makes, function (make) {
select_makes_selector.append("<option class="evaluation-make g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + make.id + "">" + make.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-makes');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Makes couldn't be fetched"))
}
});
}
function loadModels(make_id) {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-models/" + make_id + '/',
beforeSend: function () {
},
success: function (result) {
let models = result['models'];
select_models_selector.attr('disabled', false).trigger("chosen:updated");
select_models_selector.append("<option></option>");
_.forEach(models, function (model) {
select_models_selector.append("<option class="evaluation-model g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + model.id + "">" + model.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-models');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Models couldn't be fetched"))
}
});
}
</script>
The problem is with the initialisation of those select boxes. They can be initialised only once if I get it right.
With my code I get:

Thus only the first one is initialised with $.HSCore.components.HSSelect.init('.select-evaluation-makes'); in loadMakesOnPageLoad function. But that is not what I want. All three need to be initialised.
If I change brands select box, then the models select box is also initialised. But If I change the brands select box again, then the model select box doesn't work and it isn't updated. It's probably because it hits $.HSCore.components.HSSelect.init('.select-evaluation-models'); again in ajax success call of loadModels function.
If I initialise them with common class as follows: $.HSCore.components.HSSelect.init('.js-evaluation-custom-select'); they are initialised:

But then onChange event on brands select box doesn't work. If the brands select box is changed then the models select box isn't updated, probably the same reason as above. It tries to initialise it again, this time with $.HSCore.components.HSSelect.init('.select-evaluation-models');
Any idea what is going on and how can I solve it? It drives me crazy.
javascript jquery jquery-chosen
add a comment |
I'm using Unify template which has jquery chosen library for select boxes. My html code is as follows:
<div>
<select class="select-evaluation-makes js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required
data-placeholder="{% trans "Select brand" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-models js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Seect model" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-years js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Select year" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
</div>
And my JS is as follows:
<script>
const select_makes_selector = $(".select-evaluation-makes");
const select_models_selector = $(".select-evaluation-models");
const error_box = $("#error-box");
const error_message = $("#error-message");
$(document).ready(function () {
loadMakesOnPageLoad();
//Get models on make change
select_makes_selector.on('change', function (e, params) {
loadModels(parseInt(params["selected"]))
});
});
function loadMakesOnPageLoad() {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-makes/",
beforeSend: function () {
},
success: function (result) {
let makes = result['makes'];
select_makes_selector.append("<option></option>");
_.forEach(makes, function (make) {
select_makes_selector.append("<option class="evaluation-make g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + make.id + "">" + make.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-makes');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Makes couldn't be fetched"))
}
});
}
function loadModels(make_id) {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-models/" + make_id + '/',
beforeSend: function () {
},
success: function (result) {
let models = result['models'];
select_models_selector.attr('disabled', false).trigger("chosen:updated");
select_models_selector.append("<option></option>");
_.forEach(models, function (model) {
select_models_selector.append("<option class="evaluation-model g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + model.id + "">" + model.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-models');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Models couldn't be fetched"))
}
});
}
</script>
The problem is with the initialisation of those select boxes. They can be initialised only once if I get it right.
With my code I get:

Thus only the first one is initialised with $.HSCore.components.HSSelect.init('.select-evaluation-makes'); in loadMakesOnPageLoad function. But that is not what I want. All three need to be initialised.
If I change brands select box, then the models select box is also initialised. But If I change the brands select box again, then the model select box doesn't work and it isn't updated. It's probably because it hits $.HSCore.components.HSSelect.init('.select-evaluation-models'); again in ajax success call of loadModels function.
If I initialise them with common class as follows: $.HSCore.components.HSSelect.init('.js-evaluation-custom-select'); they are initialised:

But then onChange event on brands select box doesn't work. If the brands select box is changed then the models select box isn't updated, probably the same reason as above. It tries to initialise it again, this time with $.HSCore.components.HSSelect.init('.select-evaluation-models');
Any idea what is going on and how can I solve it? It drives me crazy.
javascript jquery jquery-chosen
add a comment |
I'm using Unify template which has jquery chosen library for select boxes. My html code is as follows:
<div>
<select class="select-evaluation-makes js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required
data-placeholder="{% trans "Select brand" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-models js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Seect model" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-years js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Select year" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
</div>
And my JS is as follows:
<script>
const select_makes_selector = $(".select-evaluation-makes");
const select_models_selector = $(".select-evaluation-models");
const error_box = $("#error-box");
const error_message = $("#error-message");
$(document).ready(function () {
loadMakesOnPageLoad();
//Get models on make change
select_makes_selector.on('change', function (e, params) {
loadModels(parseInt(params["selected"]))
});
});
function loadMakesOnPageLoad() {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-makes/",
beforeSend: function () {
},
success: function (result) {
let makes = result['makes'];
select_makes_selector.append("<option></option>");
_.forEach(makes, function (make) {
select_makes_selector.append("<option class="evaluation-make g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + make.id + "">" + make.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-makes');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Makes couldn't be fetched"))
}
});
}
function loadModels(make_id) {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-models/" + make_id + '/',
beforeSend: function () {
},
success: function (result) {
let models = result['models'];
select_models_selector.attr('disabled', false).trigger("chosen:updated");
select_models_selector.append("<option></option>");
_.forEach(models, function (model) {
select_models_selector.append("<option class="evaluation-model g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + model.id + "">" + model.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-models');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Models couldn't be fetched"))
}
});
}
</script>
The problem is with the initialisation of those select boxes. They can be initialised only once if I get it right.
With my code I get:

Thus only the first one is initialised with $.HSCore.components.HSSelect.init('.select-evaluation-makes'); in loadMakesOnPageLoad function. But that is not what I want. All three need to be initialised.
If I change brands select box, then the models select box is also initialised. But If I change the brands select box again, then the model select box doesn't work and it isn't updated. It's probably because it hits $.HSCore.components.HSSelect.init('.select-evaluation-models'); again in ajax success call of loadModels function.
If I initialise them with common class as follows: $.HSCore.components.HSSelect.init('.js-evaluation-custom-select'); they are initialised:

But then onChange event on brands select box doesn't work. If the brands select box is changed then the models select box isn't updated, probably the same reason as above. It tries to initialise it again, this time with $.HSCore.components.HSSelect.init('.select-evaluation-models');
Any idea what is going on and how can I solve it? It drives me crazy.
javascript jquery jquery-chosen
I'm using Unify template which has jquery chosen library for select boxes. My html code is as follows:
<div>
<select class="select-evaluation-makes js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required
data-placeholder="{% trans "Select brand" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-models js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Seect model" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
<select class="select-evaluation-years js-evaluation-custom-select u-select-v1 g-min-width-200 g-brd-none g-bg-secondary g-color-main g-color-primary--hover g-py-12"
required disabled
data-placeholder="{% trans "Select year" %}"
data-open-icon="fa fa-angle-down"
data-close-icon="fa fa-angle-up">
</select>
</div>
And my JS is as follows:
<script>
const select_makes_selector = $(".select-evaluation-makes");
const select_models_selector = $(".select-evaluation-models");
const error_box = $("#error-box");
const error_message = $("#error-message");
$(document).ready(function () {
loadMakesOnPageLoad();
//Get models on make change
select_makes_selector.on('change', function (e, params) {
loadModels(parseInt(params["selected"]))
});
});
function loadMakesOnPageLoad() {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-makes/",
beforeSend: function () {
},
success: function (result) {
let makes = result['makes'];
select_makes_selector.append("<option></option>");
_.forEach(makes, function (make) {
select_makes_selector.append("<option class="evaluation-make g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + make.id + "">" + make.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-makes');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Makes couldn't be fetched"))
}
});
}
function loadModels(make_id) {
$.ajax({
type: "GET",
url: "/vehicle-evaluation/get-models/" + make_id + '/',
beforeSend: function () {
},
success: function (result) {
let models = result['models'];
select_models_selector.attr('disabled', false).trigger("chosen:updated");
select_models_selector.append("<option></option>");
_.forEach(models, function (model) {
select_models_selector.append("<option class="evaluation-model g-brd-none g-color-main g-color-white--hover g-color-white--active g-bg-primary--hover g-bg-primary--active" value="" + model.id + "">" + model.name + "</option>")
});
$.HSCore.components.HSSelect.init('.select-evaluation-models');
},
error: function (response) {
error_box.removeClass("hidden");
error_message.html('<strong>' + gettext("Oh snap!") + '</strong>' + gettext("Something went wrong. Models couldn't be fetched"))
}
});
}
</script>
The problem is with the initialisation of those select boxes. They can be initialised only once if I get it right.
With my code I get:

Thus only the first one is initialised with $.HSCore.components.HSSelect.init('.select-evaluation-makes'); in loadMakesOnPageLoad function. But that is not what I want. All three need to be initialised.
If I change brands select box, then the models select box is also initialised. But If I change the brands select box again, then the model select box doesn't work and it isn't updated. It's probably because it hits $.HSCore.components.HSSelect.init('.select-evaluation-models'); again in ajax success call of loadModels function.
If I initialise them with common class as follows: $.HSCore.components.HSSelect.init('.js-evaluation-custom-select'); they are initialised:

But then onChange event on brands select box doesn't work. If the brands select box is changed then the models select box isn't updated, probably the same reason as above. It tries to initialise it again, this time with $.HSCore.components.HSSelect.init('.select-evaluation-models');
Any idea what is going on and how can I solve it? It drives me crazy.
javascript jquery jquery-chosen
javascript jquery jquery-chosen
asked Nov 21 '18 at 14:35
Boky
3,84733676
3,84733676
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
After you updated the select with ajax response put on the next line:
$(".select-evaluation-makes").trigger("chosen:updated");
You must trigger "chosen:updated" event as per chosen docs show
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414406%2fupdate-chosen-select-box-with-jquery-and-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
After you updated the select with ajax response put on the next line:
$(".select-evaluation-makes").trigger("chosen:updated");
You must trigger "chosen:updated" event as per chosen docs show
add a comment |
After you updated the select with ajax response put on the next line:
$(".select-evaluation-makes").trigger("chosen:updated");
You must trigger "chosen:updated" event as per chosen docs show
add a comment |
After you updated the select with ajax response put on the next line:
$(".select-evaluation-makes").trigger("chosen:updated");
You must trigger "chosen:updated" event as per chosen docs show
After you updated the select with ajax response put on the next line:
$(".select-evaluation-makes").trigger("chosen:updated");
You must trigger "chosen:updated" event as per chosen docs show
answered Nov 21 '18 at 14:45
darklightcode
1,22189
1,22189
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414406%2fupdate-chosen-select-box-with-jquery-and-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown