What is the best way to connect the user-input data with text in a span element?

Recently, I've started learning Vue.js and I'm facing an issue with binding data from an input field to a span element. Instead of appending the data, it's showing me undefined

Here is the code snippet:

new Vue({
  el: "#app",
  data: {
    counter: 0,
    x: 0,
    y: 0,
    name: "hello"
  },
  methods: {
    increment: function(step, $event) {
      this.counter += step
    },
    decrement: function(step, $event) {
      this.counter -= step
    },
    points: function(event) {
      this.x = event.clientX;
      this.y = event.clientY;
    },
    alert: function(event) {
      alert("alert!")
    },
    change: function(value) {
      this.name = this.name + value
    }
  }
});
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b2b1a184f6eaf1eaf5f2">[email protected]</a>/dist/vue.js"></script>
<div id="app">
  <input type="text" v-on:keyup.enter="change(value)" value="">
  <span>{{name}}</span>
</div>

I'm getting an error every time I press the enter key, with undefined being appended. Any help on resolving this issue would be greatly appreciated.

Answer №1

From my understanding, you are looking to display the input value within a span tag. To achieve this, you need to bind the name value to the input in the same way you've bound it to the span tag using v-model. Once you make this adjustment, any changes made within the text box will be reflected inside the span tag.

<input type="text" v-on:keyup.enter="change(value)" v-model="name" />

After implementing the change above, your code should successfully update the content of the span tag based on the input value.

new Vue({
  el: "#app",
  data: {
    counter: 0,
    x: 0,
    y: 0,
    name: "hello"
  },
  methods: {
    increment: function(step, $event) {
      this.counter += step
    },
    decrement: function(step, $event) {
      this.counter -= step
    },
    points: function(event) {
      this.x = event.clientX;
      this.y = event.clientY;
    },
    alert: function(event) {
      alert("alert!")
    },
    change: function(value) {
      this.name = this.name + value
    }
  }
});
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1761627257253922392621">[email protected]</a>/dist/vue.js"></script>
<div id="app">
  <input type="text" v-on:keyup.enter="change(value)" v-model="name" />
  <span>{{name}}</span>
</div>

Answer №2

It seems like there is a mistake in your argument for the change method. Consider using the following:

<input type="text" v-on:keyup.enter="change($event.target.value)">
.

To learn more about this, check out this resource.

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

Exploring the Node.js Connector: Utilizing Collection.remove

I can't wrap my head around the meaning of this: w, {Number/String, > -1 || ‘majority’ || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = ‘majority’ or tag acknowledges the ...

Use TypeScript to cast the retrieved object from the local storage

const [savedHistory, setSavedHistory] = useState(localStorage.getItem('history') || {}); I'm facing an issue in TypeScript where it doesn't recognize the type of 'history' once I fetch it from localStorage. How can I reassign ...

Experimenting with animating an element through onClick event using JavaScript

I would like to create an animated div using JavaScript that activates on click. <div class="Designs"> <p>Designs</p> <div class="Thumbnails" data-animation="animated pulse"> <a href=" ...

I am interested in swapping out the text links in a menu for images

Looking to swap out text links in a menu for images, but stuck with template constraints generated by rapidweaver. The HTML template can't be modified, except for the link text itself. For example: <a href="http://truehealth.gr/eng/" rel="">!UK ...

Issue with displaying multiple checkboxes using Materialize CSS in combination with Leaflet for web-mapping overlays

I'm currently using Materialize 0.97.7 along with Leaflet 1.0.1 (the latest version). <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet-src.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com ...

Send JSON information using a POST request through the Fetch API and retrieve it on the Express backend server

Currently, I am in the process of learning Express and front-end JavaScript. My aim is to pass JSON data via a POST request using the Fetch API and then handle it on the backend with Express. The backend code I have written looks like this: const express ...

Incorporate Error Text Spacing in Input Group Design

I'm currently working on developing a method to incorporate space for error messages within my form input group component. The main objective is to prevent any displacement of elements on my forms when displaying error messages. In this scenario, it ...

Storing distinct values in separate variables within a JavaScript for loop

I'm attempting to assign various values generated by an outer loop to different variables and then utilize those variables as an array. var code = []; for (i = 0; i < 5; i++) { code[i] = mp3; } Is there a way to access variables in this manner ...

An endless cycle occurs in the watcher when trying to refresh data from Vuex

I am facing an issue with a "dumb" component that receives props from a parent. The user has the ability to change a selector which triggers an action (using Vuex) to fetch new data. Once the new data is received, I need to pass it to the child component a ...

The React onChange event fails to trigger

Why isn't the onChange event firing in the input tag? I used LinkedStateMixin to track the input value before, but now I want to add an onChange event to run a function. After removing LinkedStateMixin, the onChange event still doesn't fire. I ev ...

Problem encountered when Next.js and CSRF token interact on the server

I've integrated the next-csrf library (https://github.com/j0lv3r4/next-csrf) into my next.js app to safeguard api routes. Despite following the documentation, I'm encountering a 500 error with the following message: {"message":"Si ...

"The issue with ExpressJS deleteRoute is that it is not properly refreshing user

I have a specific issue with my express controller for deleting user posts. The problem is that while the post is being removed from the page successfully, it is not getting deleted from the User.posts data as expected. function deleteRoute(req, res) { ...

What could be causing my AJAX code to fail in retrieving information from an API?

Hey everyone, I'm new here and hoping to get some help with an issue I'm facing. I've written a code to fetch data from an API and display it on my HTML page, but for some reason the AJAX code isn't working. There's nothing showing ...

Lost in a strange predicament

As I try to send props from my parent component to the child component, I am facing an issue where one of the children within the coin prop seems to be lost when I receive the props in componentWillReceiveProps(). This discrepancy becomes evident through t ...

List out IP addresses in JavaScript

Is it possible to allow users to input a range of IP addresses in a specific format? 10.5.15.[22-25],10.5.16.[35-37],10.5.17.20 The desired outcome is to return an array of these IP addresses for connection checking purposes later on. 10.5.15.22 10.5.15 ...

What is the best way to implement a never-ending scrolling grid loader within a scrollable area using Codeigniter?

In my Codeigniter framework and bootstrap installation, I have multiple sub-pages. On one of these pages, I am attempting to implement an infinite scroll loader using a jQuery script from a tutorial found at gridScrollFx.js. Here is the JS file I am using: ...

What is the process for entering a value into mysql based on the date?

Seeking assistance with a specific coding challenge. The task at hand involves inputting a date, such as '2018-05-08', and based on the user's input, storing the value in different columns of a database table. For instance, if the date is &a ...

After the initial iteration, the .length function ceases to function properly when

A validation method is set up to check the length of a user input field. It functions properly on the initial try, but does not update if I go back and modify the field. $("#user").blur(function () { if ($("#user").val().length < 3) { $("#userval ...

Trouble with comparing two datetime values in JavaScript

I have a dilemma with two different appointments: appointment1 = "2013-07-08 12:30:00" appointment2 = "2013-07-08 13:30:00" My goal in JavaScript is to compare these two appointment dates. If they don't match, I want to remove the appointment; if t ...

Requiring addresses for Google Maps in order to display directions

To display the directions between two addresses based on the chosen transportation mode, I need to pass the addresses to the code in page 2. After the user selects two cities from a Dropdown box on page 1, they will be sent to the code to show their locati ...