What is the best way to have text automatically selected upon clicking a category in a combobox? (Vue.JS 2)

I am currently working with a Vue component that looks like this:

<script>
    export default{
        template: '\
            <select class="form-control" v-model="selected" v-on:change="search">\
                <option v-for="option in options" v-bind:value="option.id" v-bind:disabled="option.disabled">{{ option.name }}</option>\
            </select>',
        mounted() {
            this.fetchList();
        },
        data() {
            return {
                selected: '',
                options: [{id: '', name: window.trans.category.select}]
            };
        },
        methods: {
            search(e){
                window.location = window.BaseUrl + '/search?q=&cat=' + e.target.value;
            },
            fetchList: function() {
                this.$http.post(window.BaseUrl+'/category/list?parent_id=all').then(function (response) {
                    response.data.forEach(function(item){
                        this.options.push({id:item.id, name:item.name})
                    }, this);
                });
            },
        }
    };
</script>

When a category is clicked, I want to retrieve the text of the category.

In my code above, I am using e.target.value to get the selected id and it works fine.

However, I am struggling to find a way to get the selected text when a category is clicked.

I tried using e.target.text, but it did not work as expected.

If anyone has a solution or suggestion, I would greatly appreciate the help.

Answer №1

If you are in need of retrieving both the id and name, consider implementing the following solution:

    template: '\
        <select class="form-control" v-on:change="search">\
            <option v-for="option in options" v-bind:value="option.id"  @click="selected=option.name" v-bind:disabled="option.disabled">{{ option.name }}</option>\
        </select>',
    mounted() {
        this.fetchList();
    },

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

What is the proper method for adding a file to formData prior to sending it to the server using a

I came across this tutorial on FormData, but I'm still trying to grasp how the formData object functions. Input Form Example: https://i.stack.imgur.com/h5Ubz.png <input type="file" id="file-id" class="w300px rounded4px" name="file" placeholder=" ...

having difficulty with executing ajax post within a JavaScript function

While working on my project, I encountered a small issue. I am able to read the HTML image value and pass it to a JavaScript method successfully. However, when trying to pass this value via AJAX POST to the controller, an unexpected error occurs. The con ...

Why isn't my output being shown on the screen?

Why is the output (refresh value) not being displayed? function myFunction() { $a = document.getElementById("examplehtml").value; document.write("<big><bold>"); document.write($a); document.write("</bold></big>"); $b = ...

What is the best way to utilize mapping and filtering distinct values in an array using TypeScript?

What is the best way to filter and map distinct elements from an array into another array? I've experimented with various methods but keep encountering a syntax error stating "Illegal return statement". My objective is to display only unique items f ...

Automatic Vue prop emitting of nested data to the parent component

While working with Vue props, I noticed something unusual. Props are supposed to have a one-way data flow (from parent to child). However, I am experiencing a strange behavior where my props seem to automatically emit changes back to the parent when using ...

What is the process of implementing a page change using a GET request in JavaScript using Node.js and Express?

Within this application, users are provided with a table where they can input data. Each row in the table is equipped with an "Edit" button that, when clicked, should redirect them to a new page labeled "/update" where modifications to the specific row can ...

Creating a class in JavaScript following an AJAX request

After receiving an AJAX response, the class "navtex-message" doesn't work properly when I create HTML with JavaScript. function formatNtxMessage( m ) { return '<div class="row">' + '<div class="col-lg-9">' ...

Leveraging asynchronous response data within asynchronous components

Here's the structure of a parent component and a child component: export default { name : 'parentNode', mounted: function () { var that = this; if (that.$store.state.auth.isAuthenticated) { that. ...

React Native error: encountering the error message "undefined is not an object '_this3.props.navigation()'"

I am encountering an error in the navigationOptions() function when running my React app, but everything is working correctly in the render() function. App.js import React, { Component } from 'react'; import { AppRegistry, View } from 'r ...

Transmitting JSON securely to a server using an HTML form webpage

Currently, I have an HTML page with a form that communicates with a server using JSON requests. The purpose is to load user's previous answers upon loading and save new answers when the submit button is pressed. Each user has a unique user_id attribut ...

Tips for deleting an image file, or any file, from a Node.js Express server

Being a novice in the field of web development, I decided to embark on creating a basic E-commerce application as my first project. I managed to make good progress until I hit a roadblock while trying to remove an image file of a product: I utilized expres ...

Concealing a VueJs component on specific pages

How can I hide certain components (AppBar & NavigationDrawer) on specific routes in my App.vue, such as /login? I tried including the following code in my NavigationDrawer.vue file, but it disables the component on all routes: <v-navigation-drawer ...

An error has occurred with Laravel-mix and Pinia: The function getActivePinia was used without an active Pinia instance

I have integrated my Vue 3 and Pinia code using the laravel-mix bundler. Here is a snippet from my app.js: require('./bootstrap'); import { createApp } from 'vue' import { createPinia } from "pinia"; const pinia = createPini ...

Definition in Typescript: The term "value is" refers to a function that takes in any number of arguments of

export function isFunction(value: any): value is (...args: any[]) => any { return typeof value === 'function'; } What is the reason behind using value is (...args: any[]) => any instead of boolean ? ...

Incorporate an external library

I am currently facing a challenge in my angular2 project where I need to import a 3rd party library. Here are the steps I have taken so far: ng new myproject npm install --save createjs-easeljs npm install @types/easeljs However, I am stuck at this poin ...

Obtaining a specific item from a group using AngularJS. Utilizing a declarative method

Consider a scenario where you need to apply a CSS rule to a specific element within a collection that needs to be selected. <div ng-repeat="fragments" ng-click="makeSelected()"></div> $scope.fragments = [ {id: 6379, someProperty: "someValu ...

Determine if the specific subroute has a child using vue-router

After checking similar questions on stackoverflow without success, I am seeking a solution. I am attempting to determine if a subroute is a child of a specific route in order to display a container. Unfortunately, the following code snippet does not work: ...

Disable form submission in MVC using a JavaScript function

In my project, there is an upload.ascx file that will be loaded inside a Jquery Popup. The upload.ascx file contains a File Upload control that uploads .xlsx and .xls files. To validate the file uploads and prevent unnecessary files from being uploaded, I ...

Customize the position of the Datetimepicker

Is there a way to customize the position of the datetimepicker, ensuring that it stays within the visual boundaries without getting cut off? I need help with this. ...

Modifying the state object in ReactJS: A step-by-step guide on updating values

Below my query and explanation, you will find all the code. I am currently attempting to update the state in a grandparent class. This is necessary due to file-related reasons, and I am using Material-UI for text boxes. Additionally, I am implementing Red ...