Im trying to run some jquery code after the template renders. But it’s not working. When I run the code in the console manually, it works. Here’s the js :
Template.PanelLayout.helpers({
restricted: function() {
return !Meteor.user();
},
authInProcess: function() {
return Meteor.loggingIn();
},
canShow: function() {
return !!Meteor.user();
}
});
Template.PanelLayout.rendered = function() {
$(document).ready(function() { // This is the code I want to run
$(".button-collapse").sideNav();
$('.collapsible').collapsible();
});
}
don’t wrap your code in a $(document).ready() method - it will never get triggered - Meteor is a single page app, the document was ready some time before your template came to be rendered
Perfect. Now it’s working. Thanks I also have another code like that. It loads without an issue. But when I visit a different page and go again to that page it doesn’t load the code. That code turn the textarea to a wysiwyg editor (TinyMCE). On the first load it works but then the editor is gone. HEre’s the code :
IIRC, onRendered isn’t called every time an {{#if}} is reevaluated. I would have to check on that. With this, you know that the elements that jQuery is looking for will be present when the onRendered is called.
Great. For that case this is a good idea. But for the next case it’s not. In my second code, the element Im trying to change through jquery is inside the content. Can’t take it out.
Ok nevermind, ive fixed it. onRendered declaration is working you just need to always refresh the tinyMCE, to do that you have to put tinymce.remove(’#note_field’); before your tinyMCE initializer. So the declaration must look like.