Search programatically in Select2 4.0 using ajax

1,238 views
Skip to first unread message

Lawrence Lagerlof

unread,
Jun 30, 2015, 5:52:58 PM6/30/15
to sel...@googlegroups.com
Hi.

I have a select2 currently working doing ajax calls. But what I want is to programatically set a string to search in that select2. I tried these below, but no one worked:

$('#myselect2').select2("val", "my string");
$('#myselect2').select2('data', {id:1, text:"my string"});
$('#myselect2').select2('data', {id:1, label:"my string"});
$('#myselect2').select2("search", "my string");
$('#myselect2').select2().val("my string").trigger("change");


("my string" exist in the database and searching by it manually works)



Thanks.

Kevin Brown

unread,
Jul 12, 2015, 6:26:48 PM7/12/15
to sel...@googlegroups.com
Select2 3.5.2 provided a `select2('search', 'term')` helper that allowed you to programmatically inject searches. This was not carried over in Select2 4.0.0, but you can find a helper function for doing this at this Stack Overflow answer.


Note that assigning data using `select2('data')` is also no longer supported in Select2 4.0.0.

--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages