Vue Js Make Text Editable on Click: In Vue.js, you can make text editable on click by using the v-on directive to bind a click event to a method that toggles the editable state of the text. When the text is clicked, the method is called and the editable state is changed, which causes the text to be rendered with an input element instead of a plain text element. The v-model directive can then be used to bind the input element’s value to a data property in the component, allowing the user to edit the text. When the input element loses focus or the user presses Enter, the editable state is toggled back and the updated text is saved. This can be achieved with only a few lines of code and is a common pattern used in web applications.
How can I make text editable on click using Vue.js?
- The HTML code defines a
divelement with anidof “app”. Within thisdiv, there are three elements:- An
h1element with arefof “heading” and acontenteditableattribute that allows the user to edit the text. The text within the element is set to the value ofcontent.heading, which is a string defined in the Vue app’s data object. - An
h2element with arefof “subheading” and acontenteditableattribute that allows the user to edit the text. The text within the element is set to the value ofcontent.subheading, which is a string defined in the Vue app’s data object. - A
pelement with arefof “paragraph” and acontenteditableattribute that allows the user to edit the text. The text within the element is set to the value ofcontent.paragraph, which is a string defined in the Vue app’s data object.
- An
- The Vue app’s
dataobject contains a single property calledcontent, which is an object with three properties (heading,subheading, andparagraph), each of which is a string. These strings represent the initial text that will be displayed in theh1,h2, andpelements, respectively. - The Vue app’s
methodsobject contains three methods:updateHeading: This method is called whenever the user inputs new text into theh1element. It updates thecontent.headingproperty with the new text.updateSubheading: This method is called whenever the user inputs new text into theh2element. It updates thecontent.subheadingproperty with the new text.updateParagraph: This method is called whenever the user inputs new text into thepelement. It updates thecontent.paragraphproperty with the new text.
Vue Js Make Text Editable on Click Example
<div id="app">
<h1 ref="heading" contenteditable v-html="content.heading" @input="updateHeading"></h1>
<h2 ref="subheading" contenteditable v-html="content.subheading" @input="updateSubheading"></h2>
<p ref="paragraph" contenteditable v-html="content.paragraph" @input="updateParagraph"></p>
</div>
<script type="module">
const app = Vue.createApp({
data() {
return {
content: {
heading: 'Editable Heading',
subheading: 'Editable Subheading',
paragraph: 'Editable paragraph text.',
},
}
},
methods: {
updateHeading(event) {
this.content.heading = event.target.innerHTML;
},
updateSubheading(event) {
this.content.subheading = event.target.innerHTML;
},
updateParagraph(event) {
this.content.paragraph = event.target.innerHTML;
},
},
});
app.mount('#app');
</script>
Output of above example



