Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

riot.js : add tag dynamically and mount it

I am very new to riot.js and may be i am asking an obvious thing.

If I add tag statically and then mount it - everything works perfectly. But if I try to add tag using JavaScript dynamically - I see nothing. I suppose that I must somehow mount newly created element, but I don't know how to do this.

<script src="https://cdnjs.cloudflare.com/ajax/libs/riot/2.6.7/riot+compiler.min.js"></script>
<body>
  <h1>
    testing riot.js
  </h1>
  
  <ol id="list">
    <li>
      <example></example>    
    </li>
    <li>
      <example></example>    
    </li>
  </ol>
  <button onclick="addTag()">Add tag</button>
  
  <script type="riot/tag">
  <example>
    <p>Welcome to Riot.js</p>
  </example>
</script>

<script>
	riot.mount('example');
  
  function addTag(){
  	var list = document.getElementById("list");
    var li = document.createElement('li');
    list.appendChild(li);
    
    var tag = document.createElement('example');
    li.appendChild(tag)
  }
</script>

</body>
like image 314
never Avatar asked Jan 16 '26 23:01

never


1 Answers

You must call riot.mount after you've added the node to the DOM:

function addTag(){
  var list = document.getElementById("list");
  var li = document.createElement('li');
  list.appendChild(li);

  var tag = document.createElement('example');
  li.appendChild(tag)
  riot.mount(tag, 'example');
}
like image 189
Antoine Avatar answered Jan 19 '26 15:01

Antoine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!