1

I just getting started with meteor so I want to use append function in jquery but it not append anything

JS:

if (Meteor.isClient) {      
Meteor.startup(function() {
        $( "span" ).append('Hello');
      });

I get no errors and nothing happens or appending.

HTML:

<span>lorem ipsum</span>
<p>stackoverflow</p>

i think the problem is $('span').append('Hello'); it loading before the html code ??

1 Answer 1

3

Yes, Meteor needs to use its renderer's own hooks for "DOM readiness". If you use Blaze (Meteor's default renderer, as of now) you can use the Template.templateName.onRendered() function for this:

Template.templateName.onRendered(function () {
  $( "span" ).append('Hello');
});

This will append "Hello" to any span the template templateName may contain.

Note that, as I implied earlier, this will be different depending on the renderer you choose for your app: Blaze, Angular or React. (or anything else for that matter) But the default one is Blaze, so if you do not know which one you are using, it is probably Blaze.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi again, well your solutions it work but somtimes when I restart the server it not append anything ?? did you know why ?
Well, what template did you assign your jquery command to? Does this template always contain a span first-hand? As in: it is clearly written within the template's html code, and not between handlebar statements such as {{#each}}?
well Im using iron router, so i have a layout template ( {{> yield}} ) and another one foo and the span element it exist foo template so i put Template.layout.onRendered.... ( i do Template.foo.onRendered but it not ork at all )
Yeah there is a {{#each}} in foo temaplate
Then if the span is in the each, try creating another template for the inside and use onRendered on this new sub-template (see this answer)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.