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

Jquery is causing some issues with the code provided:

This is the code snippet from script.js: $(document).ready(function() { $('#mainobject').fadeOut('slow'); }); Here is a glimpse of index.html: <!DOCTYPE html> <html> <head> <title>Hitler Map&l ...

Can someone please explain how to use the prevState in REACT?

Can you explain the difference between how to define the counterHandler function in these two examples? counterHandler = () => { this.setState(() => { return { times: this.state.times + 1 } }); } versus counterHandle ...

Implementation of OAuth 2 Authorization Code Grant

I currently have the following setup: Resource Server: A basic RESTful API coded in PHP (using Slim and Doctrine) Auth Server: My custom implementation of an OAuth 2 server in PHP (utilizing Slim and Doctrine) Public Client: A Single Page Application (S ...

Could somebody provide clarification on the functions being called in the Angular Test Code, specifically evaluate() and dragAndDrop()?

Exploring the drag and drop functionality of an angular-Gridster application using Protractor with a test code. I have some questions about the functions being used in the code snippet below. Can someone clarify the purpose of evaluate() - the API definit ...

ReactJS Scripts are throwing an error indicating that the function require is not defined

Just starting out with React and I discovered that we can integrate React by adding the necessary scripts to our main index.html file. I followed all the steps to include the script and even made sure to add Babel since I'm writing my main App in JSX. ...

The hierarchy of CSS in Vue components

I've developed a customized CSS framework to streamline the design process across all my internal projects. The central file is structured as shown below: @import "~normalize.css/normalize.css"; @import "_variables.scss"; @import "_mixins.scss" ...

When JSONP is used in cross-domain AJAX calls, it retrieves plain JSON data

I am facing an issue with an API that I need to integrate. The API provides JSON data, but as it involves a cross domain AJAX call, I have to utilize jsonp. $.ajax({ type: "GET", url: url + query, ...

Adding property to an object retrieved from an API in a React application can be achieved effortlessly by utilizing the useState

How can I implement a toggle functionality for Bookmarked Meals on my Meal Recipe Website available at ? I am currently using the logic to set data.meals[0].bookmarked to true or false, but I want to use setState instead in order to rerender the page when ...

Obtain the final array within an array composed of multiple arrays

My dilemma involves working with a dynamically generated array, where I need to extract only the values from the last array for aggregation purposes, discarding all others. Imagine a structure similar to this: let target = [] let data = [ [ { name: &apo ...

Update the VueJS application by loading and replacing the existing JSON data with new information

Is there a way to dynamically re-render the entire v-for loop and DOM after fetching and loading new JSON data to replace the current one? I want to be able to click on different options and have the products updated. Vue.use(VueResource); var produ ...

The TypeScript error occurs when attempting to assign a type of 'Promise<void | Object>' to a type of 'Promise<Object>' within a Promise.then() function

I'm currently working on a service to cache documents in base64 format. The idea is to first check sessionStorage for the document, and if it's not there, fetch it from IRequestService and then store it in sessionStorage. However, I've encou ...

Connect a nearby dependency to your project if it has the same name as an npm repository

What is the best way to npm link a local dependency that has the same name as a project in the npm registry, like https://registry.npmjs.org/react-financial-charts? Here is an example: cd ~/projects/react-financial-charts // Step 1: Navigate to the packa ...

Issue encountered: Unable to load Vue file after installation within node_modules

I am encountering an issue with my webpack configuration when trying to load a 3rd party plugin. I believe the problem lies in the vue-loader not being properly configured because I can successfully import my own components from the project folder. import ...

There seems to be a glitch in my JavaScript for loop as it is not iterating for the correct amount that it should

It seems like my for loop is not always iterating 7 times as intended. Sometimes it runs with 5 iterations, other times with 4 or 3. This is the JavaScript code I am using: var start = new Date().getTime(); var end = new Date().getTime(); function timeT ...

Can I add the object to the DOM and hold off on executing any events until the next

Just came across this intriguing interview question (shown below). Could someone please provide an explanation of the question and then offer a solution? Develop a function that takes an object and adds it to the DOM, ensuring that events are stored unt ...

One effective way to transfer state to a child component using function-based React

My goal is to pass an uploaded file to a child component in React. Both the parent and child components are function-based and utilize TypeScript and Material-UI. In the Parent component: import React from 'react'; import Child from './ ...

Aptana's Smart Code Completion

Does Aptana offer code completion for custom JavaScript libraries? If so, how can we enable it? ...

Purge excess bins that are not in use

I need to delete a large number of inactive bins from the system, but I can't find an option for mass deletion or inline editing using saved searches. Is there another method to accomplish this task since I have hundreds of records to delete? ...

Change the z-index of divs when clicked

When a button is clicked, I want to iterate through a group of absolutely positioned children divs with varying z-indexes. The goal is for the z-index of the currently visible div to decrease on each click, creating a looping effect where only one div is v ...

Perform Function Again

Recently, I came across some code for a tesseract ocr from Google that seemed to be functioning well. However, I encountered an issue where it would work fine the first time I input a URL, but on the second run, it wouldn't work without a manual page ...