Utilize tags as properties in Vue.js by selecting them

I am attempting to retrieve a value from a select tag and use it in an object property. Below is the HTML code:

<div id="app">
    <h3>{{title}}</h3>
    <div class="form">
        <div class="form-group">
            <div class="form-group">
                <label>Note Title</label>
                <input class="form-control" type="text" v-model="note.title">
            </div>
            <div class="form-group">
                <label>Note text</label>                        
                <textarea class="form-control" v-model="note.text"></textarea>
            </div>
            <div class="form-group">
                <label>Color</label>                        
                    <select v-model="note.color">
                            <option disabled value="">Select a color</option>
                            <option value="blue">Blue</option>
                            <option value="green">Green</option>
                    </select>
                    <span>Selected Color: {{ note.color }}</span>
            </div>
            <button class="btn btn-primary" @click="addNote">Submit</button>
        </div>
        <div class="col-sm-12">
            <div class="col-sm-4" v-for="(note, index) in notes">
                <button class="close" @click="removeNote(index)">&times;</button>
                <div class="card-block">
                    <h4 class="card-title" v-bind:style="{ color: note.color }">{{note.title}}</h4>
                    <h6 class="card-subtitle mb-2 text-muted">{{note.date}}</h6>
                    <p class="card-text">{{note.text}}</p>
                </div>
            </div>
        </div>
    </div>
</div>

Here is the JavaScript code:

var app = new Vue({
    el: '#app',
    data: {
        title: 'Notemaster',
        note: {
            title: '',
            text: '',
            color: ''
        },
        notes: [
            {
                title: 'Visit Hawaii',
                text: 'just kidding lol',
                date: new Date(Date.now()).toLocaleString(),
                color:'blue'
            }
        ]
    },
    methods: {
        addNote() {
            let { text, title, color } = this.note
            this.notes.push({
                text, 
                title,
                date: new Date(Date.now()).toLocaleString(), 
                color
            })
        },
        removeNote(index) {
            <!-- removes the selected number of items-->
            this.notes.splice(index, 1)
        }
    }
});

The value of the select tag displays correctly at

<span>Selected Color: {{ note.color }}</span>
; It successfully shows the color of the title at
<h4 class="card-title" v-bind:style="{ color: note.color }">{{note.title}}</h4>
However, I am encountering an issue when trying to create a new note. The error displayed indicates that color in this.notes.push({ ... color }) is not defined. Thank you in advance

Answer №1

The issue at hand is quite clear-cut; the variable cor has not been declared within that particular scope.

It's possible that what you intended to tackle was something along these lines:

let { text, title, cor } = this.note
                   ^^^

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

Gathering Servlet details from non-form elements

I am currently in the process of developing an application that is capable of parsing JSON strings. At this stage, I am able to input a JSON string using Java, write it to an HTML document, and then have a JavaScript program read and parse it before ultima ...

What could be the reason for my code generating the error [$http:badreq]?

I'm currently attempting to retrieve JSON data from a certain URL and am having trouble getting it to work. Despite going through the Angular documentation and other resources, I still can't pinpoint the issue due to my limited experience with An ...

Waiting for multiple asynchronous calls in Node.js is a common challenge that many developers

I'm facing a dilemma trying to execute multiple MongoDB queries before rendering a Jade template. I am struggling to find a way to ensure that all the Mongo Queries are completed before proceeding with rendering the template. exports.init = funct ...

Modifying selections within a select box generated dynamically using JQuery

Looking for help on how to delegate to a static DOM element in my current situation. I need to create a dynamic select box .userDrop when .addNew is clicked, and then have the user select an option from #secDrop, triggering a change event that calls the da ...

Tips for featuring the latest blog post at the top of a NextJS blog

I have a website built on Next JS with a blog page. The setup is correct, where the /blog page displays content based on slugs. Currently, new blog posts are appearing at the bottom of the page, under older ones. I want to reverse this so that when a new p ...

Exploring the Subfolder of Laravel and Vue

Currently, I am working on a project that involves Laravel and VueJS. The Laravel application is nested in a subdirectory (www.dominio.com/subdirectory). I have made adjustments to my Apache vHost settings so that when accessing that specific URL, it redir ...

User update function is not being triggered by Ionic 2 Express call

I am currently working on implementing a feature in my Ionic 2 program that updates a user field when triggered. The code snippet below is from my user-service.ts file: // Update a user update(user: User): Observable<User> { let url = `${this.u ...

Is there a way to extract the content length from the raw DraftJS data?

I have a system where I am storing the data from my DraftJS editor in my database as a JSON string by passing it through convertToRaw(editorState.getCurrentContent()). For example, this is how the stored data looks like in the database: {"blocks": [{"key ...

Next.js is failing to infer types from getServerSideProps to NextPage

It seems like the data type specified in getServerSideProps is not being correctly passed to the page. Here is the defined model: export type TypeUser = { _id?: Types.ObjectId; name: string; email: string; image: string; emailVerified: null; p ...

Is Javascript Functioning Properly?

Similar Question: How do I determine if JavaScript is turned off? Can we identify whether javascript has been disabled and then redirect to a different page? I am currently working on a project using JSPs, where I have integrated various advanced java ...

jQuery and HTML: Need help enabling an <input> or <select> element?

Currently, I am using Mozilla Firefox 14.0.1 and Google Chrome 20.0.1132.57 (which I believe is the latest version). The code I am working with looks like this: http://jsfiddle.net/SVtDj/ Here is what I am trying to accomplish: Type something into inpu ...

Issues with AngularJS Filters malfunctioning

After watching some AngularJS videos and attempting to apply a filter to a list of bookmark categories, I'm having trouble loading the main content. At the moment, I haven't implemented views in my code and would like it to remain view-less for n ...

Update the radio button to 'selected' (using Vue.js and Vuetify)

I am relatively new to working with Vue.js and currently facing an issue involving radio buttons. In my form, I collect various inputs and upon submitting the form, I generate a JSON file containing all the answers: However, instead of the current output ...

Dropdown Pattern with React CTA Modal

While using MaterialUI's ButtonGroup for a dropdown menu, I encountered an issue trying to set up a series of CTAs that are easily interchangeable within it. The goal is to have all components reusable and the choices in the dropdown dynamic. const C ...

How can we avoid excessive re-rendering of a child component in React when making changes to the parent's state?

In my React application, I am facing a situation where a parent component controls a state variable and sends it to a child component. The child component utilizes this state in its useEffect hook and at times modifies the parent's state. As a result, ...

Difficulty with formatting decimal points in JavaScript

I seem to be having an issue with decimal places in my code. Currently, it's showing the result as 123 123 12 but I actually need it to be displayed as 12 312 312. Is there anyone who can assist me with formatting this correctly? Here is the section ...

Unable to transfer the component between components

This is the code I have: index.js: import React from "react"; import ReactDOM from "react-dom"; import {dest, People} from './components/people'; import json from './people.json'; function initFromJson() { let names = []; for(let ...

Transforming Javascript into Typescript with node modules in Visual Studio 2015

After developing a JavaScript web app using npm and webpack, I successfully converted all the .js files to .ts using the PowerShell command found here. Now, I am looking to transition to a VS2015 TypeScript project but struggling to find guidance on how ...

When attempting to pass a token in the header using AngularJS, I encounter an issue with my NodeJS server

Error is: Possibly unhandled Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11) at ServerResponse.res.set.res.header 1. All Node.js services were functioning properly before ...

Error in Discord Bot: discord.js showing TypeError when trying to read the length of an undefined property

I'm currently working on developing a Discord bot and using CodeLyon's Permissions V2 video as a guide for reference. There seems to be an issue in my message.js file which contains the following code: require('dotenv').config(); //cre ...