How to transfer input value from textbox to a div using Vue.js

I am attempting to trigger a function when a button is clicked.

Furthermore, the value from the text box one should be displayed in a div.

Sample code:

<input v-model="textdata" type="text" class="w-full rounded">

<div class="bg-white h-10 w-full">{{textdata}}</div>

<button @click="getvalue" class="bg-green-800 rounded">RECEIVE</button>

Vue.js:

<script>
import { defineComponent } from 'vue'

export default defineComponent({
  setup() {

function getvalue(){
    console.log(this.textdata)
    }

return{
    getvalue,
}

}

 
})
</script>

Currently, the data is being displayed in the console, but the goal is to show the same data in the div element.

Answer №1

To achieve the desired functionality, you need to define two references and then update one of them based on the other upon a click event.

<script>
import { defineComponent, ref } from 'vue'

export default defineComponent({
  setup() {
    const inputValue = ref("");
    const displayValue = ref("");

    function updateDisplay(){
      this.displayValue = this.inputValue
    }

    return{
        updateDisplay,
        displayValue,
        inputValue,
    }
  }

})
</script>

<template>
  <input v-model="inputValue" type="text" class="w-full rounded">
  <div class="bg-white h-10 w-full">{{displayValue}}</div>

  <button @click="updateDisplay" class="bg-green-800 rounded" >UPDATE</button>
</template>

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

Locate the closest Vue parent component containing the template reference (Vue 3)

After a Vue template ref is initialized, my goal is to identify the closest parent Vue component. To make this functionality versatile and applicable to any template ref, I have encapsulated it within a composition function (although that's merely an ...

What are the steps to transform an object containing arrays into strings and then embed them into my HTML code?

Here is the code I need to add to my errors array and send the values to my HTML client-side template: { "email": [ "user with this email already exists." ] } I am looking for something like this: "user with t ...

What is the process for choosing a category in the freebase search widget?

For my current project, I have been utilizing the Freebase Search Widget. It allows me to select words from a suggestion list in my input box. However, I am curious about how to also obtain the category in another text box. Here is an example that demonst ...

Load the values into the dropdown list based on the selection from the previous dropdown menu

Currently, I am working on implementing cloning functionality for select boxes. There are 3 select boxes: country, state, city. The user selects the country first which then populates the state select box based on the ID, and similarly, the city dropdown ...

Leveraging Vue Data for Storing CSS Properties

I am currently utilizing the Quasar framework in conjunction with Vue for my application development. Below is a snippet of my code: <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"> Save </q-tooltip> <q-tooltip c ...

Exploring fresh opportunities within the Electron Vue.js application using router hash mode

When working on my Electron Vue.js application, I found the need to incorporate multiple windows, similar to modals on a website. To manage these windows/modals effectively, I created a service within my application. Initially, during the app development ...

Error in D3: stream_layers function is not defined

Utilizing nvd3.js to construct a basic stacked bar chart as detailed here I inserted the code provided in the link into an Angular directive like this: app.directive('stackBar', function() { return { restrict: 'A', ...

Tips on handling jsonp responses in CakePHP without using the .json extension

When working with CakePHP, the framework determines the data type to return by either checking for the presence of the .json extension in the URL or examining the Accepts HTTP header. It becomes a bit trickier when dealing with JSONP, as it doesn't a ...

Transforming three items into an array with multiple dimensions

There are 3 unique objects that hold data regarding SVG icons from FontAwesome. Each object follows the same structure, but the key difference lies in the value of the prefix property. The first object utilizes fab as its prefix, the second uses far, and t ...

how to put an end to sequential animations in React Native

Is there a way to pause a sequenced animation triggered by button A using button B? Thank you for your help! ...

Tips for creating incremental progress on a pop-up page?

I am looking for guidance on creating a page with specific functionalities. Here is what I have in mind: I want to implement a button that opens a popup when clicked. The popup should display static instructions and buttons for the user to progress throu ...

Creating a function to update data in a Node.js/MongoDB environment

Hey there! I'm new to working with nodejs and mongodb, and I'm trying to create a function that updates the win, lose, and draw record in my UserSchema. Here's my Schema: UserSchema = new mongoose.Schema({ username:'string', ...

Sorry, but we couldn't complete your request: User verification unsuccessful: email must be provided

Every time I attempt to save a user's credentials to the mongo database, an error pops up saying: "User validation failed: email: Path email is required." I am clueless as to why this issue keeps happening. It only started occurring when I added the v ...

What is preventing me from opening this local html page without an IIS Server?

Currently immersed in a passion project that can be found at https://github.com/loganhenson/jsrpg This project has been a collaborative effort with a friend, utilizing Visual Studio Professional for development and testing it on my local IIS server. Init ...

The property 'createDocumentFragment' is not defined and cannot be read in JavaScript code

I'm working on loading data from my database using ajax, but I'm facing an issue with the this method not functioning as expected. Below is a snippet of my source code: $(".cancel-btn").click(function() { var cancelArea = $('.cancel&apos ...

The problem of a static click function not working when clicked on a link. What is the solution for this

Details I am currently using a flickity slideshow that automatically goes to the next picture on a click. Within the slideshow, I have placed a button with text and a link to an external website (e.g. ). My Problem: When I click on the link, my slidesho ...

Angular and Node.js Integration: Compiling Files into a Single File

I have a question, is it possible to include multiple require js files in one file? If so, how can I create objects from them? Calling 'new AllPages.OnePage()' doesn't seem to work. To provide some context, I'm looking for something sim ...

A guide to seamlessly adding calendar events with JSON data using the powerful Ionic Native Calendar Plugin

Hello there, I am in the process of developing an Ionic app and incorporating the Ionic Native Calendar plugin. My goal is to utilize this plugin to dynamically adjust the calendar event parameters through JSON linked to a Firebase database, rather than h ...

Deleting a model using RestAPI and referencing another model

UPDATE: My professor just advised me to access the driver only from within the admin, not from the admin. Currently, I am developing a project that involves using restAPI's, and one of the requirements is that an admin should be able to delete a driv ...

Troubleshooting await and async functions in Express.js applications

How can I create an array like the one shown below? [{ "item":"item1", "subitem":[{"subitem1","subitem2"}] },{ "item":"item2", "subitem":[{"subitem3","subitem4&q ...