Hi Christopher,
I've read around based on what you've said, and I see what you're saying about it reloading etc. But no matter what I've tried, I can't get it to work. I've spent ages trying different configurations of jQuery and no matter what I do the Select2 will not reload. I've tried the way you and numerous others have suggested via the .on() method like this:
jQuery(document).ready(function(){
jQuery('#cred_form_1283_1_1').on('change','#cred_form_1283_1_1_select-children', function(){
alert($(this).attr('id'));
jQuery(this).select2({
dropdownParent: '#addChildModal',
multiple: 'true',
placeholder: 'Select Child(ren)'
});
});
});
I know the .on() method is working and the this context is correct because the alert box will fire and show the correct element ID. But the Select2 does not initialise after the ajax call still.
I've even tried binding it to the ajax success outcome like this:
$('body').on('click','#cred_form_1283_1_1_select-children', function(){
$.ajax({
type: "GET",
cache: false,
dataType: "html",
success: function(res) {
$('#cred_form_1283_1_1_select-children').select2({
dropdownParent: '#addChildModal',
multiple: 'true',
placeholder: 'Select Child(ren)'
});
}
});
});
Again, it fires but doesn't reload the Select2. I've tried adjusting the scope between the form ID, document and body, all three have the same result. If I remove the initial Select2 call then I can see the Select2 kick in on the change or click events, but still never after a reload.
I've attempted to seek help elsewhere, but no one has been able to tell me why my code won't work. The best suggestion I've had is that something Toolset related is interfering, but I can't see how that makes any sense either as all the parts work, just not together.
For reference here is the full code I'm using at the moment:
jQuery(document).ready(function($) {
$('#cred_form_1283_1_1_select-children').select2({
dropdownParent: '#addChildModal',
multiple: 'true',
placeholder: 'Select Child(ren)'
});
$('#cred_form_1283_1_1').on('click', '#cred_form_1283_1_1_select-children', function(){
alert($(this).attr('id'));
$(this).select2({
dropdownParent: '#addChildModal',
multiple: 'true',
placeholder: 'Select Child(ren)'
});
});
$("select[name='select-children'] option:first").remove();
});
I'm really stuck as to why this Select2 won't work after the ajax call, and I get you're not in a position to give me full code and I'm not asking for it. Simply help determining why something that I'm told should work, won't work in this context.