Display the selected value with JavaScript

Whenever I choose an item from a select dropdown that fetches data from an API, I then add the selected item's value to a table. Everything functions correctly except for when I click on addItem(), which adds the ID value of the selected item to the table. What I actually want to display on the table is the name_value instead of the ID. Additionally, I still need to retrieve the ID value of the selected item.

Below is the VUEJS code for the select:

<select
        id="select1"
        ref="seleccionado"
        v-model="id_equipo"
        required
>
  <option
    v-for="equipo in equipos"
    :key="equipo.id_equipo"
    :value="equipo.id_equipo"
  >
    {{ equipo.nombre_equipo }}
  </option>
</select>

Button for adding items:

<v-btn
      color="blue darken-1"
      @click="addItem()"
    >
      Agregar Equipo
    </v-btn>

Table Display:

<tbody>
  <tr
    v-for="item in rowData"
    :key="item.id_torneo"
  >
    <td>{{ item.id_torneo }}</td>
    <td>{{ item.id_equipo }}</td>
  </tr>
</tbody>

The function addItem:

addItem () {
  var equipos_torneo = {
    id_torneo: this.id_torneo,
    id_equipo: this.id_equipo,
  }
  this.rowData.push(equipos_torneo)
},

https://i.sstatic.net/8zLfi.png

Answer №1

Consider using this approach:

<template>
    <div>
      <!-- Include your select code here -->
      <select id="select1" ref="seleccionado" v-model="id_equipo" required>
        <option
          v-for="equipo in equipos"
          :key="equipo.id_equipo"
          :value="equipo.id_equipo"
        >
          {{ equipo.nombre_equipo }}
        </option>
      </select>

      <!-- Add your button code -->
      <v-btn
        color="blue darken-1"
        @click="addItem()"
      >
        Add Team
      </v-btn>

      <!-- Insert your table code -->
      <tbody>
        <tr
          v-for="item in rowData"
          :key="item.id_torneo"
        >
          <td>{{ item.id_torneo }}</td>
          <td>{{ item.id_equipo }}</td>
        </tr>
      </tbody>
    </div>
</template>

<script>
export default {
    methods: {
      addItem () {
        // Find the object in the equipos array with the selected id from the select
        let equipos_torneo = this.equipos.find(v.id_equipo === this.id_equipo);

        // If object is found successfully, add to the rowData array
        if(equipos_torneo) this.rowData.push(equipos_torneo);
      },
    },
}
</script>

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

The Date function is returning an unexpected string representation of the date

When creating a Data object using the new Date() function, I have encountered varying results. In some cases, it gives me a string like "2016-08-26T08:45:50.408Z", while in most cases, it provides this format --> "Thu Aug 25 06:57:10 PDT 2016". I am cur ...

Is there a way to automate the clicking of a button on this webpage if using .click() is not effective?

Currently, I am developing an extension that facilitates purchasing products from Flipkart during flash sales. However, on the product page, I am unable to click the "Buy Now" button using JavaScript as it returns undefined. Here is the code for the Buy No ...

How can I click the add button to show the information in the input field and display it in a <p> tag that is created using JavaScript? (without using inline JavaScript)

Is there a way to dynamically add user input to the page without using inline JavaScript and with minimal HTML? I want to create a new paragraph element through JavaScript when the user clicks a button, instead of relying on static elements in the HTML. & ...

transferring images from JavaScript to PHP

I am attempting to use ajax to transfer an image from JavaScript to PHP. I have the following HTML input: <input type="file" class="input-field" id="photo" name="photo"> Here is the corresponding JavaScript code: var photo = document.getElementByI ...

When working with a React component, utilize the array.filter() method to filter elements without expecting a return value. In cases where no elements match the filter criteria, an empty

Running into a dilemma with a React function component that involves destructuring a name parameter using useParams to specify the array of dog objects being passed in as props. Despite confirming the successful passing of the dogs array and the correct d ...

During the animation sequence, the element is seen ascending

I'm encountering a small issue with one of the elements I'm animating - it's moving up slightly during the animation process. It elevates about 2-3 pixels while the animation is playing, then snaps back down once it's completed. The el ...

When sending a request from Vue.js using Axios to PHP, the issue arises that the .value property is

There is a chat box with bb smileys underneath. Clicking on the smileys adds them to the text in the input box. However, when using axios to post, the array is empty. Manually entering the smiley bbcode works fine. <input id="txtName" @keyup.enter="ad ...

JQuery continuously firing off AJAX requests

Currently, I am experimenting with using a Jquery Ajax request to incorporate an AutoComplete feature. This involves utilizing ElasticSearch on the backend for data retrieval. This is what my autocomplete.html looks like: <!DOCTYPE html> <html l ...

Convert JavaScript code to Google Appscript

Looking for guidance on incorporating Javascript code into my Google Appscript. Here is the code snippet: I have a separate Stylesheet.html file saved. Can someone advise me on how to invoke the functions within the html file? <script> //google. ...

Tips on creating a personalized memoizeOne function that delivers the accurate data type

I've implemented a function for object memoization: import memoizeOne from 'memoize-one'; type ArrayWithOneObj = [Record<string, unknown>]; const compareObject = ([obj1]: ArrayWithOneObj, [obj2]: ArrayWithOneObj) => obj1 === obj ...

What are some ways to prevent "window.onbeforeunload" from appearing when a form is submitted?

Is there a way to prevent the alert box from appearing when submitting a form using the Submit button below? I want to avoid being prompted to "Leave this page" or "stay on this" after submitting the form. <script> window.onbeforeunload = ...

Is there a way to utilize a parameter for the user's input instead of relying on document.getElementById when incorporating a button?

let totalScore = 0; var myArray = [1, 2, 3, 4, 5]; let randomNum; function NumGuess(userInput) { var userGuess = userInput; var i; for (i = 0; i < 1; i++) { var randomNum = myArray[Math.floor(Math.random() * myArray.length)]; } if (us ...

Error: npm encountered a socket timeout issue

Setting up a fresh React application in /home/abhijith/Desktop/React/firstReact/myapp. Installing necessary packages. This process may require a few minutes. Installing react, react-dom, and react-scripts along with cra-template... npm ERR! code ERR_SOCK ...

Displaying an image directly in front of the camera using Three.js

I am currently working on a flight simulator project and faced a challenge in placing a cockpit image in front of my camera controlled by FirstPersonControls. Despite trying various methods, I have not been successful in making it work. Here is what I hav ...

Using Jquery to implement multi-step forms with real-time ajax confirmation

Good day, I am encountering an issue with my code. In essence, what I am attempting to do is validate the database connection and if successful proceed to the next step, otherwise remain on the same step and display an alert. However, even when the connect ...

Is there a way to stop the YAML parser from including question marks in its output?

Looking to create a basic docker compose setup and integrate it into an existing file. The yaml parser found here seems promising, but I'm struggling with the question mark that appears in its output. This is the content of my current docker compose ...

Deactivate a button on a specific tab

My setup includes two tabs: <div class="modal-body"> <form name="myForm" novalidate="novalidate"> <ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#basicInfo">Info</a></li> ...

Upon completing the upload, dropzone.js displays checkmark and x icons for confirmation

It seems like there may be a CSS problem. When I programmatically create a dropzone box, I notice that the checkmark and x icons, along with other text, appear after the upload (see image link). <div id="header-dropzone"></div> $("#header-drop ...

What is the method to display my input value within my application interface rather than triggering an alert popup?

Hi there, I am seeking assistance with my GUI code. I want the input value to be displayed as text on the screen, or even better, to look like a chatroom so that I can integrate a simple chatbot later on. My main goal is to have the texts show up in the bl ...

Instructions on retrieving basic array information from Firebase and fetching it

Being a beginner, I am currently focusing on learning Firebase instead... Here is a simple HTML code snippet that I created: <div class="container" id="msgs"> </div> I am trying to load data from F ...