callback on initSelection

1,598 views
Skip to first unread message

Brad Lilly

unread,
Jul 9, 2012, 10:09:04 AM7/9/12
to sel...@googlegroups.com
Hi There,

A bit stuck on using a callback on initSelection where I need to retrieve a single value back from an ajax call.

I added this patch here.

https://github.com/alanho/select2/commit/af1c39807611538db65886a227e9bf7227f4adba

All seems OK and I get a valid JSON response (below) however the <span> tag is empty.

{"results":[{"id":"4255","text":"Party Tour"}]}


I was working off the sample in the above url for a result.

My code sample is below. Any help would be appreciated.

$('#activitycats').select2({
placeholder: 'Search for an activity',
ajax: {
url: "/ajax/categoriesactivities.php",
dataType: 'json',
quietMillis: 100,
data: function (term, page) {
return {
term: term, //search term
page_limit: 10 // page size
};
},
results: function (data, page) {
return { results: data.results };
}

},
initSelection: function(element, callback) {
return $.getJSON("/ajax/categoriesactivities.php?id=" + (element.val()), null, function(data) {
if ($.isFunction(callback)) {
return data;
}
});
}

});













Igor Vaynberg

unread,
Jul 9, 2012, 10:12:27 AM7/9/12
to sel...@googlegroups.com
you need to return just the array portion, no need for the "results" key.

-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/IoxeDWvz-7cJ.
> To post to this group, send email to sel...@googlegroups.com.
> To unsubscribe from this group, send email to
> select2+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/select2?hl=en.

Brad Lilly

unread,
Jul 9, 2012, 10:41:17 AM7/9/12
to sel...@googlegroups.com
Thanks Igor I  tried that as below. Still no luck.


[{"id":"4255","text":"Party Tour"}]


Igor Vaynberg

unread,
Jul 9, 2012, 10:51:24 AM7/9/12
to sel...@googlegroups.com
this part is also wrong:

if ($.isFunction(callback)) {
return data;
}

should instead be

callback(data);

-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/RJQTe7zBoScJ.

Brad Lilly

unread,
Jul 9, 2012, 11:17:22 AM7/9/12
to sel...@googlegroups.com
Thanks Igor tried that as well and not working for me. Quite tired now and off for some sleep. But if you have any more ideas I would welcome them. A fresh start in the morning might give me results.


On Tuesday, 10 July 2012 00:09:04 UTC+10, Brad Lilly wrote:

Brad Lilly

unread,
Jul 9, 2012, 6:41:39 PM7/9/12
to sel...@googlegroups.com
Thanks Igor a bit of sleep did the trick. I still had an array of values and after running firebug worked out the problem.
Needed it as below without []
{"id":"4252","text":"Abseiling"}


I did see some people asking for PHP examples of server side did you want some of these?




On Tuesday, 10 July 2012 00:09:04 UTC+10, Brad Lilly wrote:

Igor Vaynberg

unread,
Jul 10, 2012, 12:50:44 AM7/10/12
to sel...@googlegroups.com
On Tue, Jul 10, 2012 at 1:41 AM, Brad Lilly <chickp...@gmail.com> wrote:
> Thanks Igor a bit of sleep did the trick. I still had an array of values and
> after running firebug worked out the problem.
> Needed it as below without []
> {"id":"4252","text":"Abseiling"}
>
> I did see some people asking for PHP examples of server side did you want
> some of these?

would be cool to have some on the wiki, yes.

thanks,
-igor
> --
> You received this message because you are subscribed to the Google Groups
> "select2" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/select2/-/QT98I7rmQnoJ.

Brad Lilly

unread,
Jul 11, 2012, 6:48:52 AM7/11/12
to sel...@googlegroups.com
Thanks Igor. This works for me... Hope it can help others.


On Tuesday, 10 July 2012 00:09:04 UTC+10, Brad Lilly wrote:
select2_sample.php

Nico Williams

unread,
Aug 26, 2025, 2:08:24 PM8/26/25
to select2
Guideatour is a convenient platform for travelers to discover guided tours and unique experiences around the world. With just a few clicks, users can find and book the perfect activities for their trips. It makes every journey more organized, exciting, and personalized.

Phill Jones

unread,
Oct 16, 2025, 2:20:29 PM10/16/25
to select2
Los requisitos de la Beca Rita Cetina buscan garantizar que el apoyo llegue a estudiantes comprometidas con su educación. Para postularse, las candidatas deben estar inscritas en una institución educativa, mantener un buen promedio y demostrar necesidad económica. Asimismo, deben entregar la documentación requerida y respetar los plazos establecidos en la convocatoria oficial.

Brahim Diaz

unread,
Nov 8, 2025, 12:32:59 PM11/8/25
to select2
GTA Mzansi APK is a popular mobile adaptation of the classic Grand Theft Auto game, tailored for South African players. It offers open-world gameplay, missions, and vehicles with local-themed elements. Players can enjoy an immersive action experience right on their Android devices.

Isaac Glover

unread,
Nov 14, 2025, 4:55:23 AM11/14/25
to select2
 The issue you’re facing with Select2’s initSelection is similar to ensuring data renders correctly when pulled from dynamic sources. Platforms like thefameplanet show how properly structured and mapped JSON can display cleanly once processed. Likewise, your callback must correctly translate the returned {id, text} pair so the <span> element reflects the selected value without remaining empty.  

Joseph

unread,
Nov 22, 2025, 9:05:20 PM11/22/25
to select2
  I've been really impressed with the quality and variety available at Gander Mountain products. Whether you're looking for camping essentials, hunting gear, or just reliable outdoor equipment, they have such a great selection. I recently picked up some items for a weekend trip and everything held up perfectly. It's nice to find a place that truly understands what outdoor enthusiasts need!  

mehar abdullah

unread,
Dec 25, 2025, 3:47:19 AM12/25/25
to select2
  I’ve been really impressed with the quality and variety offered on the Texas Roadhouse menu. Whether you’re craving hearty steaks, classic sides, or flavorful appetizers, there’s a great selection to choose from. I recently tried a few dishes on a visit, and everything was fresh, satisfying, and perfectly prepared. It’s refreshing to find a place that truly understands what food lovers are looking for. website   

Crumbl Lineup

unread,
Dec 28, 2025, 11:30:34 AM12/28/25
to select2
The taste of crumbl cookie menu is amazing! The Crumbl cookies next week is change every of monday. This one is best for cookies if you select it. 

Facebook lite apk

unread,
Dec 30, 2025, 1:59:28 AM12/30/25
to select2
  View your Starbucks Gift Card balance anytime—online, in the app, or in-store. Get quick guidance on reloading, using, and managing your card smoothly.  

Telelatino Apk

unread,
Dec 31, 2025, 1:48:28 AM12/31/25
to select2

I’ve been using Tele Latino iOS lately, and it’s been a smooth experience for streaming on my phone. The app runs well and has a good selection of content. You can check it out through tele latino apple.

Facebook lite apk

unread,
Jan 13, 2026, 1:05:32 PMJan 13
to select2
Astute Beta Server is testing platform that claims to give players early access to upcoming features, new characters, weapons, and game modes. astute beta server apk

Crumbl Lineup

unread,
Jan 16, 2026, 1:17:21 AMJan 16
to select2

I really impress this section of group and my try its, know the apk veer is the best option and must be try the ck33 game this one is interesting for you

Crumbl Lineup

unread,
Jan 31, 2026, 5:13:27 AMJan 31
to select2
   Designed for beginners and experienced users alike, 3Patti Blue Download offers an easy way to play Teen Patti online anytime, anywhere.  

Teguh Mulyantoro

unread,
Feb 25, 2026, 2:56:55 PMFeb 25
to select2
https://groups.google.com/a/tensorflow.org/g/keras-meetings/c/XcfB-H2FwBo
https://groups.google.com/g/select2/c/4NvcxT2OvL0
https://groups.google.com/g/saibersys-company-hotlist/c/SfEBOytUaPE
https://groups.google.com/g/ar-jobs/c/9pG3Xc41Ebo
https://groups.google.com/g/aspnet-group/c/GNP05Ctw5JM
https://groups.google.com/g/webxl-hotlist-candidates/c/wZd3qKoSjgk
https://groups.google.com/g/autocontrol_app/c/RHbxHZ2N8Gc
https://groups.google.com/g/best-consultants-hotlist/c/ygP6Sy0KwQY
https://groups.google.com/g/businessanalystjobs/c/Dg6c1c1uwLY
https://groups.google.com/g/corp2c-requriments/c/x1HngiLOrGc
https://groups.google.com/g/connectingrecruiters/c/ML6iyJ_610I
https://groups.google.com/g/corm-2-corp/c/xWk6WyW1F8Y
https://groups.google.com/g/direct-client-c2c-requirements/c/TcwhtVxMCXo
https://groups.google.com/g/dcro/c/Z6CB2Z7w93w
https://groups.google.com/g/entry-level-it-jobs-in-us-h1b-visa-sponsor/c/OC3mQRgZuUY
https://groups.google.com/g/erpconsultants/c/m9EU8uQa6Rc
https://groups.google.com/g/hillsidena/c/K2n2oK7UaBg
https://groups.google.com/g/itstaffingservices/c/TY-babozMfI
https://groups.google.com/g/job-hot-list/c/abOVPBcY-R0
https://groups.google.com/g/jobs-in-us/c/7MJFHU6wGTg
https://groups.google.com/g/mlwg-mobile/c/O6pT6jy13s4
https://groups.google.com/g/new-ed-talk/c/zobnT2wk9gg
https://groups.google.com/g/only-c2c-req/c/euVohuIdCA8
https://groups.google.com/g/iappz123/c/mnj6uyBl6Ws
https://groups.google.com/g/recruitersantosh/c/EB7lISfCY3k
https://groups.google.com/g/requirement2010/c/0LtxnknYJKs
https://groups.google.com/g/requirementgooglegroupscom/c/WrXBpVh_Xd8
https://groups.google.com/g/ajaxscaffold/c/LsPYERiDqg4
https://groups.google.com/g/saibersys-company-hotlist/c/0iwurSw9Zbw
https://groups.google.com/g/sap-job/c/Mc4a7jSVfeU

https://groups.google.com/g/select2/c/eID3bPGAc5I
https://groups.google.com/g/thaparece/c/EZJjNb7h-m0
https://groups.google.com/g/trade-btwn-c2c/c/A_qEeIav-1g
https://groups.google.com/g/us-it-recruiter-staff/c/Ntp0C9574EE
https://groups.google.com/g/pdusrecruiter/c/amMIYnJSBGg
https://groups.google.com/g/usa-staffing-hummer/c/kXtqz0L_5-o
https://groups.google.com/g/webxl-hotlist-candidates/c/etiFjy6Wuds

Crumbl Lineup

unread,
Feb 25, 2026, 10:27:52 PMFeb 25
to select2
  Because Crumbl cookies are rich and generously portioned, they can be high in calories and sugar. Many customers choose to split cookies into halves or quarters to enjoy them more moderately. Nutritional information is typically available in-store or online for those who want to make informed choices.  

sheh

unread,
Feb 26, 2026, 3:49:21 AMFeb 26
to select2
Don’t want to miss a single match? Sportzy APK brings the action straight to your phone. Whether it’s cricket, football or basketball. this app lets you watch everything live without any hassle. With Sportzy APK, you get smooth streaming, quick loading, and an easy-to-use interface that makes watching sports more enjoyable. You can catch live matches, check highlights, and follow all the major tournaments all from one simple app.

Crumbl Lineup

unread,
Feb 27, 2026, 5:08:18 AMFeb 27
to select2

Delta Executor is a popular third-party Roblox script executor used to run custom Lua scripts inside Roblox. It’s mainly used by players who want to modify gameplay, use custom UI scripts, or experiment with game mechanics beyond the standard Roblox experience.  

Phill Jones

unread,
Mar 7, 2026, 10:59:21 AM (8 days ago) Mar 7
to select2
Krnl Executor 2026 is an updated script executor known for its smooth performance and reliable functionality. It provides users with improved compatibility and faster script execution for a better experience. With regular updates and a simple interface, Krnl Executor 2026 remains a popular choice among script users.

Mubarra Jailani

unread,
Mar 9, 2026, 5:21:52 PM (6 days ago) Mar 9
to select2
 Just like selecting the perfect item enhances any experience, discovering unique treats keeps fans excited. Weekly rotations bring new surprises while favorites stay beloved. Paying attention to variety and presentation makes every visit enjoyable. Explore the latest Crumbl flavors to see how creativity and consistency delight every customer.  

Mubarra Jailani

unread,
Mar 10, 2026, 7:48:13 AM (5 days ago) Mar 10
to select2

This discussion about Select2 configuration and dropdown behavior is very useful for developers working on content-heavy websites. Efficient, searchable dropdowns and AJAX-powered selections can significantly improve navigation when users need to filter large datasets such as categories, characters, or chapters. I’ve seen similar UI improvements used on gaming information sites where structured navigation is essential for exploring guides, updates, and storyline sections. For example, a fan resource covering poppy playtime chapter 4 could benefit from Select2 to organize character lists, puzzles, and chapter details in a more user-friendly way. Overall, optimizing form components like this plays a big role in enhancing usability and making large websites easier to explore.

Reply all
Reply to author
Forward
0 new messages