I am currently working on my first project using the Durandal framework to create a basic website. I have encountered an issue while trying to incorporate a simple JavaScript function. My goal is to execute the function after the DOM has loaded, but I am unsure how to connect it to the existing viewmodel. The problem arises when the function gets called before the DOM loads, and the targeted div ID "sb-search" does not yet exist.
My initial attempt was to include the function in the viewmodel:
define(["plugins/router"], function (router) {
var vm = {
viewAttached: viewAttached
};
return {
router: router
};
function attached() {
new UISearch(document.getElementById('sb-search'));
}
return vm;
});
Unfortunately, this approach did not work as intended.
If anyone could offer assistance, I would greatly appreciate it. Even a straightforward tutorial demonstrating how to use "document.write('Hello World')" would be beneficial. Thank you!