Solved

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

Posted on 2014-12-24
2
9,088 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now