Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do i get the selected values from a select 2 that allows multiples

Posted on 2014-12-24
2
Medium Priority
?
12,257 Views
Last Modified: 2014-12-25
Hi

The following code worked until multiple was set to true. I presume it worked before because selected value was a single value whereas now it is an array

<!DOCTYPE html>

<html>
    <head>
        <link href="js/select2.css" rel="stylesheet"/>
        <meta charset="UTF-8">
        <title>Select2 tests</title>

    </head>
    <body>
        <script src="js/jquery-1.11.1.min.js"></script>
        <script src="js/select2.js"></script>

<input type='hidden' id='tags' style='width:300px'/>
 <p>Selected IDs: <span id="selectedID"></span></p>
<p>Selected Options: <span id="selectedText"></span></p>       
        <script>
$("#tags").select2({
createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} },
multiple: true,
data: [{id: 0, text: 'story'},{id: 1, text: 'bug'},{id: 2, text: 'task'}]
});

$("#tags").change(function() {
    //var theID = $(test).val(); // works
    //var theSelection = $(test).filter(':selected').text(); // doesn't work
    var theID = $("#tags").select2('data').id;
    var theSelection = $("#tags").select2('data').text;
    $('#selectedID').text(theID);
    $('#selectedText').text(theSelection);
});
        </script>
    </body>
</html>

Open in new window


Many thanks
0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40517052
It gives you an array of the selected values, if you want it as json to post to another page then you do

var selections = JSON.stringify($("#tags").select2('data')))

Open in new window

0
 

Author Closing Comment

by:andieje
ID: 40517847
merry christmas x
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question