question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature request: dynamically append/remove DOM elements in template

See original GitHub issue

As far as I understand, currently, once a vm instance has been created, compiled and mounted, the structure of its template is unchangeable – we can only switch some DOM elements on/off via existing directives like v-show, v-if and v-for (which are already written in the template), but there is no way to dynamically append brand-new elements/child components (which are not written in template in advance) into the template (or into the DOM).

If I am right – sometimes we do need to dynamically twist the template itself, because the way we twist it is determined by user’s random input and cannot be foreseen.

Or there already is a way to achieve this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
yyx990803commented, Dec 2, 2016

The idea here is, do you know ahead of time what possible types of content there are? If yes, then everything should be expressed in your templates or render functions, because that keeps your component’s render output declarative and predictable. You don’t have to follow through chains of imperative code to figure out why something is happening.

If you don’t know the content, then you can use v-html with something like:

userContent: ['<div>hi</div>', '<div>bye</div>']
<div v-for="content in userContent" v-html="content"></div>

^ Be careful about XSS though.

0reactions
PocketNinjaDesigncommented, Feb 4, 2018

Heyup, I want to be able to do this Codepen Popup Test I’ve asked this of React Developers in Stack Overflow and rather than giving me some sort of direction to go in, they just say, “Noo, you can’t think like jQuery anymore”, Well let me tell you, I’m thinking like Javascript and I want to be able to put any element anywhere on the page I want. There is a nicer version of that script on my site Code is on my Github account

So does this v-html look like it shows promise…?

Thank you in advance Vue type people 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically ADDING and REMOVING Components in Angular
I have created a demo to show the dynamic add and remove process. Parent component creates the child components dynamically and removes them ......
Read more >
Creating, Removing, and Cloning DOM Elements - KIRUPA
Learn how to dynamically modify the structure of your DOM by using createElement, removeElement, remove, cloneNode, and more!
Read more >
The Content Template element - HTML - MDN Web Docs
The HTML element is a mechanism for holding HTML that is not to be rendered immediately when a page is loaded but may...
Read more >
How to dynamically create JavaScript elements with event ...
This tutorial shows you how to dynamically create JavaScript elements using event handlers and template literals.
Read more >
How to Dynamically Add/Remove Table Rows using jQuery
To tackle the first problem we will use delegation and then we can handle the events of a dynamically added element. In order...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found