Rendering v-html in VueJS requires a delayed binding value that can only be triggered by clicking inside and outside of a textarea control

I'm currently experiencing an issue with a textarea that is bound to a v-model with a specific value. The problem arises when the content of the textarea is changed via JavaScript - the displayed value, represented by {{{value}}}, doesn't update immediately. Instead, I have to click into and out of the textarea for the change to take effect. You can view a live example in this JSFiddle link.

Below is the HTML code snippet:


        <div id="app">
      <textarea name="test" id="textarea" cols="30" rows="10" v-model="content"></textarea>
      <hr>
      <button @click="insertTag">insert strong tag</button>
      {{{ content }}}
    </div>
And here's the JavaScript portion:

            new Vue({
        el: '#app',
      data: {
        content: 'this is the initial content data'
      },
      methods: {
        insertTag: function(){
                var textel = document.getElementById('textarea');
          textel.value = textel.value + '<em>this is em</em>';
        }
      }
    })

Answer №1

The fundamental concept of VueJS is to manipulate data, rather than directly affecting the value of an html widget.
The addTag method should look something like this:

addTag: function() {
   this.data = this.data + '<strong>this is bold</strong>';
}

For a live example, check out this link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I implement a jQuery popup that prompts users to log in or register if they are not currently authenticated?

My JavaScript code includes jQuery and AJAX functionality for a specific action. Whenever a user id is not provided, and there isn't one stored in the session, I aim to prompt the user with a dialog box asking them to either register or log in. Could ...

Google Cloud's implementation of the "Vue+S3+Lambda" architecture

My VueJs single page website currently has very few transactions, prompting me to explore implementing it with a serverless architecture. One recommended approach for a basic Web Application on AWS includes: Vue App uploaded on AWS S3 Backend connection ...

Once invoked by an ajax request, the $().ready function is executed

The functionality of this code is flawless when running on its own. However, once I make an ajax call to it, the code fails to execute. I suspect that the issue lies within $().ready, but I haven't yet identified a suitable replacement. Any suggestio ...

Introducing the new default in Google Chrome: Now accessing window.opener for target="_blank" links after rel="noopener" is the standard feature

Exploring a new attribute called rel="noopener", I discovered that it keeps opened windows unaware of their opener. After some experimentation, I consistently found window.opener === null, only to realize that this is now the default behavior in ...

What is the correct method for dynamically importing framer-motion in NextJS?

I am currently utilizing framer-motion in my NextJS project. My goal is to import {motion} using Next's dynamic import method. However, I am encountering difficulties as it does not seem to function properly. import { motion } from "framer-motion ...

The asynchronous ajax request is leading to a browser freeze

In the HTML page, I have two sets of a and p elements that are initially set to display:none. At the bottom of the page, there is a function being called with their respective ID's and values, which will enable one of them based on certain conditions ...

Tips for structuring JSON data to retrieve numerous values

Creating a tool where users can enter their postcode to check for nearby windfarms is my current project. I have organized the data by named locations, and it's important to maintain that structure due to the specific API mapping tool I am using. Here ...

Do I need to include the :key attribute in my v-for loops?

While browsing, I came across information suggesting that the :key attribute in Vue 3 is not always mandatory in a v-for loop. Could someone confirm if this is true? When exactly do we need to use a key in v-for loops and when can we skip it altogether? An ...

Looking to display all items once the page has finished loading

I am experiencing a minor issue. Every time I access my store page where all products are listed, I have to click on the size filter to load the products. This is not ideal as I want all products to be displayed automatically when the page loads. What modi ...

How can eslint be used to enforce a particular named export?

Is there a way to use eslint to make it mandatory for JavaScript/TypeScript files to have a named export of a specific name? For instance, in the src/pages folder, I want all files to necessitate an export named config: Example of incorrect usage src/page ...

What is the best way to require users to click one of the toggle buttons in a form?

Is it possible to require the user to click one of the toggle buttons in a React form? I want to display an error if the user tries to submit the form without selecting a button. Even though I tried using "required" in the form, it didn't work as expe ...

jQuery custom slider with advanced previous and next navigation capability for jumping multiple steps

I am currently learning jQuery and programming in general. Instead of using a pre-built plug-in, I decided to create my own image slider/cycle from scratch to keep the code concise and improve my skills. My function goes through each li item, adding a &ap ...

Giant Slide - navigate directly to a particular slide using a link

Hey there, I am currently working on incorporating the Superslide slider for fullscreen images in my website. My goal is to have a mostly text-free site where users can navigate through the images using the main menu or jump to a specific image within the ...

Using JavaScript, enter the value into the input field and then redirect the

I'm encountering a minor issue with my contact form and jQuery redirection based on input fields. Everything was working smoothly until I attempted to integrate the redirection into the form validation code. Redirection snippet: $("#signup").submit ...

Automated pagination integrated with automatic refreshing of div every few seconds

I am currently working on setting up a datatable and I need it to be displayed on the monitor in such a way that it can refresh the div and automatically paginate to the next page. However, whenever the div is refreshed, the auto-pagination gets cancelle ...

Pause the counter based on the data attribute containing multiple values

I have a collection of div elements, each with a unique data-attribute value. My goal is to display these values in the divs using JavaScript by incrementing a counter. const numbers = document.querySelectorAll(".number"); console.log(numbers); let c ...

AngularJS - Ensuring the <script> tag is included only after all directives have been rendered

Forgive me if this question has been asked before. I've spent quite some time looking for a solution to no avail. I'm in the process of converting an existing application, which relies heavily on jQuery, to utilize AngularJS. However, I've ...

Effortlessly deleting a row with JQuery and PHP without reloading the page

I am currently working on creating a jQuery function to delete a div, but I'm facing an issue. Whenever I click the submit button, the div is not being removed and the page gets refreshed. How can I achieve this without refreshing the page? You can ...

Vue.js filtering by number

Looking to implement filtering in a Vue project. I have an input field that currently filters by store name extracted from a JSON object, which includes both the store name and ID as a number. How can I also filter by store ID? computed: { filtered ...

What are the steps to initiate mongodb within this particular application?

Recently, I received an assignment from this company to integrate MongoDB into a Node.js application. Since I haven't worked with MongoDB before, I'm unsure how to establish a connection with the database in the application. My main goal is to su ...