Using Vue.js to set both v-model and v-bind:value on one input element

I have a React component that has a form for submitting user information.

The main issue I'm facing is setting a default value in the input field. When the page loads, I want the input field to display the user's existing email address by default and update only if the user decides to input a new email address.

I understand that you can't use both onChange and value on the same <input> field. So, what approach should I take to achieve this functionality?

<input
  type="email"
  name="email"
  id="email-field"
  onChange={handleChange}
  value={emailAddress}
/>
export default {
  props: { defaultEmail: String },
  data() {
    return {
      emailAddress: this.defaultEmail || ''
    };
  }
}

Answer №1

Simply use v-model to connect the input value:

const contactComponent = Vue.component('contactComponent', { 
  template: '#contactComponent',
  data () {
    return {
      emailAddress: this.defaultEmail || ''
    }
  },
  props: { defaultEmail: String },
});

new Vue({
  el: "#app",
  components: { contactComponent }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <contact-component default-email="example@email.com"/>
</div>

<template id="contactComponent">
  <input
    v-model="emailAddress"
    type="email"
    name="email"
    id="email-field"
  />
</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

Can you pinpoint the origin of the circular reference that triggers the error when using JSON.stringify?

I am encountering an issue where passing an object to JSON.stringify is resulting in the error "Converting circular structure to JSON," but I cannot pinpoint the exact reason for this. The object is being passed via server-side node.js. app.get('/&a ...

Navigating using passing data in ReactJs

The following data contains information about people: const people = [ { img: 11, name: "Ahmed", job: "developer", }, { img: 13, name: "Kazim", job: "Engineer", }, ...

Remembering data in a lazy loaded AngularJs list

Encountering an issue with a lazy loaded list of books. Initially, 10 books are loaded and if the user scrolls down, the next 10 books are loaded. For example, if the user is on "page" 4 and clicks on the 35th book to view details, upon returning to the l ...

Issue in Angular: Attempting to access properties of undefined (specifically 'CustomHeaderComponent')

I have encountered a persistent error message while working on a new component for my project. Despite double-checking the injection code and ensuring that the module and component export logic are correct, I am unable to pinpoint the issue. custom-header ...

Exploring the possibilities of combining Vue 2 with Quasar framework

Embarking on a new project using Vue 2 and Quasar has been exciting for me. I began by executing npm install -g @quasar/cli, followed by quasar create app-name. Upon exploring the created application, I noticed the presence of the setup(){ } function wit ...

Using select tags in conjunction with JavaScript can add dynamic functionality to your website

I've been working on adding dropdown menus to my website and have been using code similar to this: <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</opti ...

What exactly does the term "new.target" refer to?

The ECMAScript 2015 specification references the term new.target a total of three times - once in section 14.2.3: Although Contains typically does not analyze most function forms, it is specifically used to identify new.target, this, and super usage wit ...

Ways to stop React from refreshing the page upon clicking the submit button

Is it possible to prevent a React component from reloading the page when a submit button is pressed? Here is an example of component code: class MyComponent extends React.Component<IEditCampaignStateProps & IEditCampaignDispatchProps, EditCampaignStat ...

Creating a javascript variable in c#

Currently, I am working on incorporating the selected index text from a DropDownList into a JavaScript string. My approach involves storing the text in a hidden field and retrieving it through C# to ensure the JavaScript variable retains its value even aft ...

Hold off on running the code until the image from the AJAX request is fully loaded and

Currently, I am facing a challenge with my code that aims to determine the width and height of a div only after it has been loaded with an image from an AJAX request. The dimensions of the div remain 0x0 until the image is successfully placed inside it, c ...

Tips for sharing a global variable across numerous functions in various files

<script> var words = new Array(); words[1] = 'fresh'; words[2] = 'ancient'; </script> <script src="scripts/validation.js" type="text/javascript"></script> Additionally, in the validation.js file, we find: fu ...

Issues with appending new rows using JavaScript

I'm facing an issue with adding rows to the table using this code and I can't seem to find a solution. function add() { document.getElementById("popup").style.display = "block"; document.getElementById("add").addEventListener("click", func ...

Verify the presence of the promotion code and redirect accordingly

I have created a special promotion page that I want to restrict access to only users who have received a unique code from me via email. To achieve this, I have designed the following form: <form accept-charset="UTF-8" action="promotion.php" method="po ...

Tips for calculating the total of an array's values

I am seeking a straightforward explanation on how to achieve the following task. I have an array of objects: const data = [ { "_id": "63613c9d1298c1c70e4be684", "NameFood": "Coca", "c ...

Initiating a YouTube video with a click on its thumbnail image - a jQuery tutorial

I am currently working on code that successfully displays YouTube videos. However, I would like the video to start playing when the cover image is clicked. How can I achieve this functionality? Thank you for your help! <div id="video" style="display: ...

Import the CSV file and store it in a designated directory using JQuery

My goal is to enable users to upload a CSV file from an HTML page and have it saved to a specified local directory upon upload. Two possible scenarios could unfold: if the uploaded file already exists, it should be overwritten; otherwise, a new file shoul ...

angularjs currency conversion tool

Is it possible to choose only 3-4 currency values from a drop-down list, where the selected value will determine the base URL for fetching JSON data? For instance, if I select USD as the first value, the JSON data should be retrieved from . ...

How to calculate large integer powers efficiently in JavaScript using the mod

I'm currently on the lookout for a reliable JavaScript algorithm as I attempted to implement one using node.js : function modpow_3(a,n, module){ var u = BigInt('1'); var e = equals(a, u); if( e) return a; if(equalsZero(a)) return a; if(pair ...

Updating an Object in vue.js Upon Click Event to Add a New Value

I currently have a code snippet that looks like the following: arr = [ { val1: a, val2: b }, { val1: a, val2: b }, { val1: a, val2: b } ] <div v-for="single in arr"> <button v-on:click="addSome"></button> </div> When I c ...

Password Field Validation in React

<TextField id="outlined-basic" label="Password" variant="outlined" /> Can anyone assist me in implementing password validation using an onchange function? I am seeking help with the ...