0

I feel uncomfortable having to create elements scattered everywhere and change them one by one when variable is changed.

    <div>
         <div class="element-1"></div>
         <div class="element-2"></div>
    </div>

    <script>
    var e1= "";
    var e2= "";

    SomeEvent.trigger(function(){
        e1= "this is content of element-1";
        e2= "this is content of element-2";
    });


    $("value of e1").change(function(){
        $(".element-1").html(e1);
    });
    $("value of e2").change(function(){
        $(".element-2").html(e2);
    });
    </script>

Can I do something like it only with js or jQuery?

<div>
    {{e1}}
    {{e2}}
    {{e3}}
</div>

And {{e?}} is binding with e?. It show e?'s value and change everywhere e? changes. Thanks.

T. Short
  • 2,995
  • 9
  • 26
Mido
  • 321
  • 1
  • 10
  • [Listening for variable changes in JavaScript](https://stackoverflow.com/q/1759987/2796249) – Ahed Kabalan Dec 22 '19 at 18:47
  • @AhedKabalan Thank you, but this is not the main problem. I want to replace the value at {{e?}} with the value of e? without adding any html elements around it. – Mido Dec 22 '19 at 18:53

1 Answers1

1

Use mustache and place your {{varibles}} in the template then pass data to them.

Kamran
  • 503
  • 6
  • 15