testing if input has select2 applied

8,269 views
Skip to first unread message

ffab...@gmail.com

unread,
Oct 9, 2012, 1:47:03 PM10/9/12
to sel...@googlegroups.com
How do I test if a input was transformed into a select2 input?

I'm trying to clear all inputs from a page, but I need to know if the input been cleared is a select2 input, so I can do something like this:
$(this).val('');
$(this).select2('destroy').select2(select2_options);

Thanks,

Igor Vaynberg

unread,
Oct 9, 2012, 9:26:31 PM10/9/12
to sel...@googlegroups.com
there is currently no convenient way. pull request is welcome.

-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/-/UOefKrHJVRoJ.
> 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.
>

natha...@gmail.com

unread,
Nov 15, 2012, 11:32:50 PM11/15/12
to sel...@googlegroups.com, ffab...@gmail.com
Here's how I'm currently checking if Select2 has been applied to an input, where 'this' is the input element:

$('#s2id_' + $(this).attr('id')).length > 0


The jQuery datepicker library adds a 'hasDatepicker' class to the input when it is applied, and removes the class when the widget is destroyed. It would be convenient if Select2 could do the same thing with a 'hasSelect2' class.

aguidr...@gmail.com

unread,
Dec 30, 2012, 2:08:47 PM12/30/12
to sel...@googlegroups.com, ffab...@gmail.com
вторник, 9 октября 2012 г., 20:47:03 UTC+3 пользователь ffab...@gmail.com написал:
var s2 = $(this).data('select2');
if (s2)
console.log(s2.val());

nestor...@gmail.com

unread,
Mar 30, 2014, 11:54:07 AM3/30/14
to sel...@googlegroups.com, ffab...@gmail.com, aguidr...@gmail.com
Thanks that solution helped me a lot

aleksandr.de...@gmail.com

unread,
Oct 20, 2015, 10:13:44 AM10/20/15
to select2, ffab...@gmail.com
This worked for me
$(selector).hasClass('select2-hidden-accessible')
Reply all
Reply to author
Forward
Message has been deleted
0 new messages