Total quantity based on user input

Recently embarking on my Vue journey, I'm curious about how to implement a feature where the input data takes a number and displays it in a variable. Additionally, if the number is entered twice, I want to add them up.

<template>
    ...
</template>

<script>
export default {
        data(){
        return{
            myMoney:null,
            dollar:0
        }

    },
    methods:{
        addMomey(){
            this.myMoney.push(this.myMoney) 
        }


    }
}

I attempted to achieve this functionality but ended up with a direct transfer to a variable instead.

Answer №1

Explore this link:

<script>
export default {
  data() {
    return {
      myMoney: 0,
      dollar: 0
    }
  },
  methods: {
    addMoney() {
      this.dollar += this.myMoney
    }
  }
}
</script>

<template>
  <input v-model.number="myMoney" />
  <button @click="addMoney">Add
  </button>
  <div>
    dollar: {{this.dollar}}
  </div>
</template>

If you prefer using enter key, enclose the content in a form and use @submit.prevent, try out this method:

<form @submit.prevent="addMoney">
  <input v-model.number="myMoney" />
  <button type="submit">Add</button>
</form>

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

Presentation with multi-directional animations

Curious to know if it's possible to create a unique slideshow that scrolls in multiple directions? The concept is to display various projects when scrolling up and down, and different images within each project when scrolling left and right. Is this i ...

Variability in Focus Behavior while Opening a URL in a New Tab with window.open()

Here is a code snippet I have been using to open a URL in a new tab: window.open(urlToOpen, '_blank', 'noopener noreferrer'); The issue I am experiencing is that when this code is executed for the first time, it opens the URL in a new ...

Guide for inserting a button into a Django list for transferring data from the list to an HTML TextField

I'm seeking an optimal approach to include data from the Django model in a view, displayed in a template within a list format. The goal is to use a button alongside other information on each row to trigger this action. Specifically, I aim to have a p ...

Determine the placement of a <div> based on information stored in localStorage

I'm diving into the world of localStorage and trying to figure out how it works. To get a better understanding, I decided to create an HTML page that allows users to drag and drop tiles around the screen. Here's a snippet of the code I came up ...

Rendering D3 graphs using VueJS

Having encountered incompatible dependencies, I am reluctantly downgrading from vue3 to vue2. In vue3, I successfully created a force directed graph using the D3 library with the composition API. However, transitioning my graph to vue2 has proven to be a c ...

Issue with selecting file not triggering input file selection event in KnockoutJS and HTML when the same file is selected

Struggling to find a solution using KnockoutJS instead of jQuery. When the same file is selected, the event doesn't fire. Here is an example of the HTML: <label class="upload"> <input id="documen ...

Utilize Element UI autocomplete to transfer data into a VueJS-powered table's input fields

I am currently working on designing an invoice form using the Element UI framework. I have successfully integrated an autocomplete feature that retrieves data from the loadAll array. Upon clicking the add button and selecting an item_name from the autocomp ...

Angular 6: Issue with displaying data on the user interface

Hello! I am attempting to fetch and display a single data entry by ID from an API. Here is the current setup: API GET Method: app.get('/movies/:id', (req, res) => { const id = req.params.id; request('https://api.themoviedb.org/ ...

How to efficiently upload multiple files in an array using jQuery and AJAX technology

Looking for a way to upload each file separately with HTML? <input type="file" name="[]" multiple /> Struggling to figure out how to achieve this? Here is an example: $('input:file').on('change', function(){ allFiles = $(th ...

Reset all modifications made by jQuery animations before initializing a fresh animation

Is there a way to reset the changes made to the first div before animating the second div using jQuery animation? $(".order-b").click(function(event){ $(".order-t").animate({top:'30%'}, "slow" ); }); $(".counsel-b").cl ...

The AJAX request is failing to retrieve the id and pass it along to the PHP script

I manage a page where I review and either accept or deny new users. Once a user is accepted, they are directed to a section labeled Accepted Users, where the admin can modify their permission level or group number. In the following code snippet, I am retri ...

"Efficiently calculate the total sum of columns in a datatable using dynamic JavaScript across

For further clarification, this question is an extension of a previous inquiry, which can be viewed here. In the following code snippet, I am calculating the column sum of a Shiny datatable using Javascript in order to display it directly below the table. ...

Potential PHP/Javascript Concurrency Issue

As a newcomer to the world of web programming, I am still learning about Javascript, PHP, Ajax, and more. Despite my efforts to find a simple solution to a particular issue through Google searches, I have hit a roadblock. It seems like there might be a rac ...

The alignment of elements in the div seems to be slightly skewed

Currently, I am in the process of creating a website with the temporary name yeet.io. I am facing an issue where I am attempting to center both an input and h1 element inside a div vertically and horizontally, but they keep appearing misaligned for some r ...

Concealing a paragraph by utilizing v-if within vuex, while also placing it inside a designated tab

I have a store with an array that I'm looping through to create individual pages with shared content. Each page has a button that hides a paragraph. How can I ensure that the paragraph is only hidden on the first page and not on the others? I want the ...

What is the best approach to displaying multiple instances of a single class in React, similar to the layout used in platforms such as Instagram, Facebook

I am looking to display a component (card) showcasing a product multiple times. The data for the component is pulled from a database, so all I need to do is render the <Product /> component with different props. In the image below you can see the com ...

Using Axios to fetch data and populating select dropdown options in a Vue component

I am currently working on integrating the response data values with my multiselect options. Although the console log displays the returned results, I'm facing difficulty in connecting them to my multiselect options. When I enter 'Day' into ...

Disregard all numbers following the period in regex

I have developed a function to format numbers by adding commas every 3 digits: formatNumber: (num) => { return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') }, The issue with this function is that it also add ...

Is there a way to prevent certain code from executing during server deployment?

First of all, my apologies for any mistakes in my English language skills. I find Morgan to be a great tool for development, but I am uncertain about deploying my server and not wanting everyone to see who is online. How can I prevent certain actions fro ...

The issue with Next.js Incremental Static Regeneration causing changes to not appear on the page until manually reloading

Currently working on incorporating Incremental Static Regeneration into a Next.js project. The index page displays a list of posts with the revalidate: 1 parameter in the getStaticProps() function. Another page contains a form for editing post titles. Up ...