When you call loadTeachers()
on DOMReady the context of this
will not be the #CourseSelect
element.
You can fix this by triggering a change()
event on the #CourseSelect
element on load of the DOM:
$("#CourseSelect").change(loadTeachers).change(); // or .trigger('change');
Alternatively can use $.proxy
to change the context the function runs under:
$("#CourseSelect").change(loadTeachers);
$.proxy(loadTeachers, $('#CourseSelect'))();
Or the vanilla JS equivalent of the above, bind()
:
$("#CourseSelect").change(loadTeachers);
loadTeachers.bind($('#CourseSelect'));