How to add dynamic HTML attributes in VueJS ?
This article is about how to add dynamic HTML attributes in VueJS . The v-bind helps us to add any attribute to our HTMl while working in vue.
In this article, we will learn How to add dynamic attributes in VueJS.
In vue, adding a dynamic attribute to HTML is very easy. In vuejs, we have v-bind directive, which is very powerful and can provides us with attribute binding capabilities.
What does v-bind do in Vuejs?
The v-bind directive is used to bind one or more attributes in our HTML template. It also helps to bind a component prop to an element too.
You can use v-bind with its shorthand version : .
Using v-bind you can bind class, style, id, src, and even href in your HTML.
Add dynamic src attribute in HTML using v-bind
To add dynamic src attribute in your<img /> tag in HTML, you can just bind it as:
<img v-bind:src="imgSrc" /> //OR <img :src="imgSrc" />
If you want to add a dynamic class or style property to your div, you can do it using v-bind.
V-bind a dynamic class attribute in HTML
You can bind more than one class in your HTML template.
<div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }" ></div> <script> export default { name: "App", data() { return { isActive: true, hasError: false }; }, }; </script>
Here in the example, we have added active and text-danger classes dynamically using v-bind.
When isActive or hasError changes, the dynamic classes will be added accordingly.
Add dynamic inline styles in HTML
The object syntax of v-bind:style helps us to add CSS to our template, except it's a JavaScript object.
Use camelCase for the style property names.
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div> <script> export default { name: "App", data() { return { activeColor: 'red', fontSize: 30 }; }, }; </script>
Add dynamic class as Object in Vuejs
You can also use v-bind to add Object to HTML template.
<div v-bind:class="classObject"></div> <script> export default { name: "App", data() { return { classObject: { active: true, 'text-danger': false } }; }, }; </script>
These are some of the few examples of how you can add class, style, and an image src attribute dynamically in your HTML code using VueJS. There are a lot more, which you can read on their VueJS Binding Documentation.
Related Topics:
How to add common header and footer in vuejs.
Related Posts
Easy Way to use localStorage with Vue
Short article on how to use local storage with Vue and learn saving and storing data in local storage with the setItem() and getItem() methods.
Force update Vue to Reload/Rerender component
Short tutorial on how to correctly force update Vue.js component to reload or rerender. The component is reinitialized without having to do a browser refresh.
Set and get cookies in a browser in Vue App
Find out how to set and get cookies in a browser for your webpage in Vue using vue-cookies package.
Save vuex state after page refresh in Vue App
Short tutorial on how to save (or persist) data in a vue application using Vuex and vuex-persist npm package.
