How to set ReactiveVar with value of "input text"?

I want to set reactive value of input box

// Template
<template name="myTpl">
   <input type="text" id="myVal" value={{myVal}}>

   <select id="mySelect">

// Js
var myVal = ReactiveVar(0);
   myVal: function(){
      return myVal.get();
   'change .mySelect': function(e, t){

1- input text = 0 // for the first time
2- input text = 100 // when change the select box for the first time
3- I change the input text = 500 by manual
4- And then I change the select box again, but input text still store the old 500 (don’t 100)

cause you never changed myVal when typing in input text, so when it have 500 and you try to assign it 500, it will not invalidate.

you need to monitor also change (keyup) on input text and set myValue to it’s value. It is not 2way binding.

Thanks for your explain.
I will try.

This works

    this.reactive_var = new ReactiveVar(0);

    counter: function () {
      return Session.get('counter');
    reactive_var: function(){
      return Template.instance().reactive_var.get();
    'click button': function (e,t) {
      // increment the counter when button is clicked
      Session.set('counter', Session.get('counter') + 1);
      // set reactive_var
    'change input': function(e,t) {
1 Like

I believe you need to change your selector in your events.

You’re listening for a class instead of an id.{
   'change #mySelect': function(e, t){ // Change this

This is the pattern I’m using with ReactiveVars too.

Look great, It work fine.
Thanks all :smiley: