Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : How to select DOM elements within EJS file

$
0
0
First, please get used to using the console to log things - alerts are for other things.  Next, I would make a strong argument for doing what you are trying to do from your controller.  The controller should be able to render your template and take care of any post-rendering logic you need.  However, I know you are going to reply with "my app really really needs this to work", so alas, wrap it all in a setTimeout:

setTimeout(function() {
    console.log("Outer HTML foo length: " + $('#foo').length);
    console.log("Inner EJS div length: " + $('#divtest').length);
}, 0); 

While you might assume that a 0 timeout says "do this immediately (synchronously)", but there is actually waits until the current task (thread) is done executing... meaning `#divtest` will actually exist in the DOM tree by the time the timeout function executes.  If you still run into problems, try bumping up the timeout, or move your logic to the controller so that you know for sure that the template is loaded and is in the DOM.

Viewing all articles
Browse latest Browse all 3491

Trending Articles