Is it possible to programmatically add custom attributes to an element using plain JS, similar to
element.attr('data-attr', someValue)
? But what about Vue directives like v-if
?
Imagine we have the following element
<p v-html="data.title"></p>
How can a v-if directive be added programmatically? This question arises with automation in mind, especially when dealing with numerous dynamic variables that may or may not exist.
The desired result is
<p v-if="data.title" v-html="data.title"></p>
The one step I am aware of is to access the element in the created()
lifecycle hook using a ref
.