Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery in Meteor 1.0

Tags:

jquery

meteor

I am trying to use jquery like this in meteor.js app.

JS:

    if (Meteor.isClient) {      
    Meteor.startup(function() {
            $( "button" ).click(function() {
              $( "p" ).toggle();
            });
          });
...

Or without meteor.startup function. Neither works.

HTML:

<button>Click</button>
<p>Can you see me?</p>

I get no errors and nothing happens when I click the button.

like image 444
Mika Avatar asked Nov 25 '14 00:11

Mika


2 Answers

You shouldn't use jQuery for simple event handling like this, use Meteor templates event maps instead :

HTML :

<template name="myTemplate">
  <button type="button">Click me !</button>
  <p>Can you see me ?</p>
</template>

JS :

Template.myTemplate.events({
  "click button":function(event, template){
    template.$("p").toggle();
  }
});
like image 190
saimeunt Avatar answered Oct 11 '22 23:10

saimeunt


use meteor list to see if jquery package has included.
if not, use meteor add jquery to add the package

like image 7
Bird Eggegg Avatar answered Oct 12 '22 00:10

Bird Eggegg