Modify form content by utilizing a VueJS method

Currently, I am utilizing VueJS and facing a challenge with a form that consists of two fields. The first field requires user input, while the second field should calculate its value by passing the input from the first field through a method.

HTML

<div id="app">
    <input type="text" v-model="value.label">
    <input type="text" v-model="value.slug"> 
  <!-- Slug value to display here -->
  <br /><br />
  {{value}}
</div>

Javascript

new Vue({
    el:'#app',
    data:{
        value:{
            label: '',
            slug: ''    // This value is calculated by passing label to santiize()
        }
    },
    computed: {
    },
  methods: {
   sanitize (label) {
    return label+'something'
   }
  }
});
  1. The scenario involves the user entering information into the first field, updating value.label

  2. We aim to pass value.label through the sanitize() method in order to update value.slug. The new value should reflect immediately in the form field. However, I am uncertain about how to achieve this. If the user leaves the form field blank, an automatic value based on the description should be generated.

  3. In addition, it would be beneficial to allow the user to manually override the result returned by the sanitize function by typing their own value in the form field. Hence, if the user decides to enter a custom value, it will take precedence over the calculated one.

I have prepared a fiddle for reference - https://jsfiddle.net/9z61hvx0/8/

Answer №1

After making some adjustments to the data structure and implementing a watcher for 'label', I successfully resolved the issue...

new Vue({
    el:'#app',
    data:{
        label:'',
    slug: ''
    },
    computed: {
    computedSlug () {
    return this.value.label+'Manish'
    }
    },
  watch: {
    label: function () {
        this.slug = this.sanitize(this.label)
    }
  },
  methods: {
   sanitize (label) {
    return label+'something'
   }
  }
});

I am open to any other advanced solutions as well :)

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

TypeScript Error: The Object prototype must be an Object or null, it cannot be undefined

Just recently, I delved into TypeScript and attempted to convert a JavaScript code to TypeScript while incorporating more object-oriented features. However, I encountered an issue when trying to execute it with cmd using the ns-node command. private usern ...

The problem with using the Transition group in Vue arises when trying to implement it within nested

<transition-group name="slide"> <section-tree v-for="(section,index) in form.sections" :key="section.random" :index = "index" :section = "section" @selectedSectionAndLesson="selectedSectionAndLesson" > ...

Steps for saving both checked and unchecked checkbox values in Firebase

I have a set of objects that I am using as initial data in my Ionic 3 application: public interests = [ { name: 'Travel', checked: false }, { name: 'Animals', checked: false }, { name: 'Theatre', checked: false ...

When the button is clicked, display in the console the data retrieved from the API

Programming Section import react, { useEffect } from 'react'; import './styles.css'; export default function App() { useEffect(() => { fetch('https://jsonplaceholder.typicode.com/todos') .then((response) => ...

Dynamic resizing of charts in Angular using ChartJS

I am currently working on a stacked bar data chart using Chart.js 2.0.2 and Angular-Chart 1.0.0 to display some information. My goal is to identify when the user reaches the bottom of the chart container, so that I can dynamically add more data while maint ...

The desired result is not appearing when using a `fetch` request, but it does show up with an

I am facing an issue with two different requests - one using ajax and the other using fetch, its successor. When I send my data using fetch to the same API as ajax, I notice that the results are not consistent. Ajax $.post(this.config.baseUrl + this ...

The verification feature in ASP.net and Javascript

In this scenario, I am facing a challenge. I have an ASP page containing a form and an action button. When the user clicks the button, a confirmation box (confirm) should be displayed to prompt the user. If the user clicks OK, action A should be triggered; ...

Navigate within an HTML element by utilizing the element's unique ID

I need to create a condition based on the presence of style="display:none;" in the following code snippet. <div class="wrap hide" id="post_query" style="display:none;"> </div> My goal is to identify whether style="display:none;" is included o ...

I am in need of some guidance - where can I locate the documentation for vue cli

Searching for specific build modes tailored to my Vue CLI 2 application has proven to be a challenge. Despite consulting various resources, all the information I come across pertains to Vue CLI 3. After attempting to locate documentation for Vue CLI 2, u ...

ReferenceError: source is not defined in Gulp with Browserify

I encountered a strange error while working on my project. The error message I received is as follows: $ gulp browserify [01:21:03] Using gulpfile F:\CSC Assignments\FinalProject\HotelProject\gulpfile.js [01:21:03] Starting 'bro ...

creating a fresh window using AJAX in Ext JS

While I know there may be similar questions out there, the details of my situation are unique. I'm facing an issue with building a new window using ExtJS, starting from a pre-existing grid. The goal is to populate this new window with references to el ...

Using checkboxes in an Express application

Currently, I am working on the task of parsing checkbox values into an array using express/ejs. Users are required to fill out a form and select checkboxes as shown below: Answer: Checkbox: The goal is to create two arrays from the input data: answer = ...

Passing a value from Javascript to PHP using Ajax in CakePHP 3.x results in an empty $_POST variable

Despite successfully alerting the post data with Ajax ({"booking":{"testdata":"testdata"}}), the issue arises when the PHP code fails to retrieve $_POST. Both JavaScript and PHP are present on the same page: Take a look at my code below: <script type ...

Tips for sending information from a controller to jQuery (Ajax) in CodeIgniter

Code snippet in controller: $rates['poor'] = 10; $rates['fair'] = 20; $this->load->view('search_result2', $rates); //Although I have attempted different ways, the only successful method is using the code above. Other ...

Differences between Selenium WebElement.click() and triggering a click event using Javascript

I am curious about the distinctions between utilizing the click() method of the WebElement and locating the element by id to trigger the click event with JavaScript. To clarify, the first approach involves calling the .click() on an instance of WebElement ...

The significance of using `jshint globalstrict: true` alongside 'use strict'

It's quite common for me to come across these two lines at the beginning of JavaScript source code. /* jshint globalstrict: true */ 'use strict'; While I understand the significance of 'use strict';, I am curious about why jshint ...

Utilizing cookies to track the read status of articles - markers for reference

Currently, I am in the process of developing a website and am interested in implementing a feature that allows users to track which articles they have read. I am considering adding a small circle next to each article heading to signify whether it has been ...

A step-by-step guide on generating orders and order line items in Odoo with the help of Node.js

Struggling to create orders and add order-lines to the odoo database? Getting stuck with an error message faultString: ('The requested operation ("create" on "Sales Order" (sale.order)) was rejected because of the following rules:\\n\&b ...

Unable to send a post request using axios to my Laravel application

I've attempted multiple times to send an AJAX request with Axios using Vue.js to my Laravel application. I made sure to include the csrf_token() in the master Blade meta tags. However, I keep receiving a status of 419 (Unknown Status). If anyone has e ...

Is there a method for eliminating divs that have scrolled out of view on a never-ending webpage in order to prevent lag?

Looking for a solution to remove divs that have been scrolled past in Chrome in order to improve speed on an endlessly scrolling page. The sluggishness is making the page unusable. ...