Change button text in Meteor with JS

狂风中的少年 提交于 2019-12-25 07:15:04

问题


My button on click show and hide div that contains comment section. Now I want to make him to change text on click. So, you click once and you can see comments but instead of 'Show comments' hes text now needs to be 'Hide comments'. I tried several solutions that I found on internet and a couple of solutions that were logic to me but it's not working. I tried this too but it says that SetSession is not defined.

Template:

<template name="PrikažiMe">

<button class="PrikažiKomentar"> {{текст}} </button>

</template>

JS

if (Meteor.isClient) {


  /*  Template.PrikažiMe.onCreated(function() {
        Session.set(текст , 'Прикажи коментаре');  // <---- This part makes like everything is unpublished
    }); */


    Template.PrikažiMe.events({
      'click .PrikažiKomentar': function(){

        if (document.getElementById(this._id).style.display == "none") 
            { document.getElementById(this._id).style.display = "inline-flex", SetSession (текст, 'Сакриј коментаре');}
        else {document.getElementById(this._id).style.display = "none", SetSession (текст, 'Прикажи коментаре'); }
      }, 
    });  

    Template.PrikažiMe.helpers({   
      текст: function(){
        return Session.get(текст);
      },
    });      
};

回答1:


You shouldn't change HTML/CSS from JavaScript, instead handle that in your template.

Here's an example with untested code. It also relies on the package reactive-var.

<template name="t">
  <button id="b">{{#if isButtonActive}}Active!{{else}}Not active{{/if}}</button>
</template>
Template.t.onCreated(function(){
  this.isButtonActiveReactiveVar = new ReactiveVar(false)
})

Template.t.helpers({
  'isButtonActive': function(){
    return Template.instance().isButtonActiveReactiveVar.get()
  }
})

Template.t.events({
  'click #b': function(event, template){
    template.isButtonActiveReactiveVar.set(!template.isButtonActiveReactiveVar.get())
  }
})



回答2:


So, I found out what was the problem. Hope this will help someone in the future. Look at the code:

Template:

<button class="PrikažiKomentar"> {{#if текст}}Сакриј коментаре {{else}} Прикажи коментаре {{/if}} </button>

</template>

JS:

if (Meteor.isClient) {

    Template.PrikažiMe.onCreated(function PrikažiMeOnCreated() {
        this.текст = new ReactiveVar(false);
    }); 

    Template.PrikažiMe.events({
      'click .PrikažiKomentar': function(event, template){
        if (document.getElementById(this._id).style.display == "none") 
            { document.getElementById(this._id).style.display = "inline-flex", template.текст.set( true );}
        else {document.getElementById(this._id).style.display = "none", template.текст.set( false );}
      }, 
    });  

   Template.PrikažiMe.helpers({   
       текст: function() {
              return Template.instance().текст.get();
       },
    });      
};


来源:https://stackoverflow.com/questions/37538555/change-button-text-in-meteor-with-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!