Solved

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

Posted on 2014-12-24
2
9,916 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
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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)

785 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