Step-by-step guide on how to display chosen values in a multi-select dropdown menu

How can I dynamically select values in a multi-select box based on an array using Vue.js? I've attempted a solution but it's not working as expected. Any suggestions or help would be greatly appreciated.

<div id="app">

<select class="multiselect" v-model="mySelect" multiple class="multi-size">
      <option>Access</option>
      <option>BA Briefings</option>
      <option>Change Management</option>
      <option>Clearance</option>
      <option>Communications</option>
      <option>CONUS</option>
      <option>Delay</option>
      <option>Disruption</option>
      <option>eQIP</option>
    <option>Field Ops</option>                                                                                                               
  </select>  


  <button @click="add">Add selection</button>
</div>


new Vue({
  el: "#app",
  data() {
    return {
     mySelect:'',
     selData: ["Access", "BA Briefings", "CONUS", "Delay"]
    }      
  },
  methods: {
    add: function(){
      this.selData.forEach(data=>{
        this.mySelect = data;
      })
    }
  }
})



Check out this example pen.

Answer №1

If you want to enable multiple item selection in a multiselect element, make sure to define the mySelect as an array and add the elements to the array according to your selection. Below is the updated code snippet for your reference:

new Vue({
  el: "#app",
  data() {
    return {
     mySelect:[],
     selData: ["Access", "BA Briefings", "CONUS", "Delay"]
    }      
  },
  methods: {
    add: function(){
      this.selData.forEach(data=>{
        this.mySelect.push(data);
      })
    }
  }
})
<div id="app">
<select class="multiselect"  v-model="mySelect" multiple  class="multi-size">
      <option>Access</option>
      <option>BA Briefings</option>
      <option>Change Management</option>
      <option>Clearance</option>
      <option>Communications</option>
      <option>CONUS</option>
      <option>Delay</option>
      <option>Disruption</option>
      <option>eQIP</option>
      <option>Field Ops</option>
  </select>  
  <button @click="add">Add selection</button>
</div>

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

How can data be transferred between web pages using jQuery or JavaScript?

Imagine having two different pages on a Classified car website: The first page is the search page, which displays a list of all cars. Check out a sample search page here The second page is the details page, where you can find specific information about a ...

The image is experiencing difficulty loading from the Express static directory

Having some trouble with image loading... I've noticed that images are loading fine from the local folder, but not from the uploads folder which is set to be static. I've been attempting to upload a file from the browser. The upload and save pr ...

Create a validation rule in yup that depends on an external condition

I am currently attempting to set a condition based on a prop that is passed to a VueJS object. After researching some solutions, I noticed they all focus on using .when. However, .when is typically used when you need to establish a condition based on anot ...

Content briefly appears and then vanishes with the use of ng-if

On my webpage, I have content that is enclosed in an ng-if directive as shown below: <div ng-if="ShowMessgae" class="text-danger"> <p> <strong> Message displayed to User </strong> </p> < ...

AngularJS: Unable to preserve the data

I'm currently working on an issue with saving updated functions using angularJS. I've managed to edit the data and update it on the database side, but the changes aren't reflecting on the frontend side unless I logout and login again. I need ...

Convert the class to a file upload using jQuery

I am currently working on a project involving a file upload script. I have multiple directories where the files need to be uploaded, and I'm attempting to use a single form for this purpose. So far, I have been able to change the form class using jQu ...

Only implement $(document).on(...) for every JQuery request

As someone new to Jquery, I'm facing challenges in grasping its functionality. Specifically, my struggle lies in setting up an event listener on an i element with the class of .dataPickerIcon, which should respond to a click by inserting more HTML usi ...

Sending specific names as properties

Looking to streamline my repetitive form inputs by abstracting them into a component, I want the following functionality: <InputElement title="someTitle" v-model="someName" @blur="someFunction" name="someName" type="someType"> The desired output wo ...

Is your window.location.hash malfunctioning?

I am facing an issue with changing the background color of a <div id="services> when a specific link (index.html#services) is clicked. I have attempted to use the latest jQuery along with the jQuery Color plugin from jQuery Color. The code snippet I ...

Receiving a null value when attempting to access the ids of dynamically created HTML elements using JavaScript

Encountering issue with accessing ids of dynamically generated HTML elements using javascript In my current project, I am attempting to dynamically alter the ids of div elements and remove buttons that are appended to the parent div. The desired functiona ...

Troubleshooting why content set to a <div> element with JavaScript / jQuery is not persisting after a

This is the current code snippet I am working with: <asp:Button ID="btnSave" runat="server" OnClick="Save" CssClass="StylizedButton" resourcekey="btnSave" /> <div id="lbltot"></div> Below is the JavaScript portion of the code: $(do ...

The loading of the Bootstrap tagsinput has encountered an error

I am facing an issue with my Django application where tags are not loading properly in an input field using jquery. It seems like the application is unable to locate the bootstrap-tagsinput.css and bootstrap-tagsinput.js files. Can anyone provide guidance ...

The data from my client side AJAX request is not being received by my server side PHP script

Check out the AJAX code I've written: $("#loginbutton").click(function(){ var email = $('#email').val(); var password = $('#password').val(); $.ajax({ url: 'login.php& ...

`Async/await: Implementing a timeout after a fadeout once the fadein operation is

Here is a snippet of code that demonstrates the process: async function loadForm (data) { let promiseForm = pForm(data); await promiseForm.then(function(data) { setTimeout(function () { $container.fadeOut(function() { ...

Ways to display or conceal text using Vanilla JavaScript

I am struggling to toggle text visibility using Vanilla JS. Currently, I can only hide the text but cannot figure out how to show it again. <button id="button">my button</button> <div> <p id="text">Hello</p& ...

What could be causing the issue of the view not showing up in AngularJS?

I've been trying to use AngularJS modules to display a view, but for some reason my page isn't showing up. Can anyone help me figure out what's going on? You can check out my code at the following link: <!DOCTYPE html> <html> & ...

Adding dots between page numbers when using ReactJS/Javascript Pagination can be achieved by implementing a specific method

I have created a pagination component using React JS. Currently, it only displays all the page numbers, but I want it to show dots when there are more than 8 total pages. Here's how I envision it: c. When total is less than 9 pages: Page 1 selecte ...

Scrolling to a specific element using jQuery after a specified delay has

On my website, I have a page with an autoplaying video located at . My goal is to implement a feature where once the video completes playing after a specific duration, the webpage will automatically scroll down to the text section. This scroll action sho ...

What is the procedure for closing a snackbar when the close button is clicked?

I am having trouble closing a snackbar when the close button is clicked. The snackbar should initially pop up on page load and only close when manually triggered. I have set the timeout to zero, but the snackbar does not close when the close button is clic ...

To ensure a rectangular image is displayed as a square, adjust its side length to match the width of the parent div dynamically

How can I make the images inside my centered flexbox parent div, #con, be a square with side length equal to the width of the parent div? The image-containing div (.block) is positioned between two text divs (#info and #links). I want the images to be squa ...