Programmically clear Remote Select2 box

2,549 views
Skip to first unread message

owr...@gmail.com

unread,
Nov 6, 2012, 10:46:52 AM11/6/12
to sel...@googlegroups.com
I asked the question on SO(http://stackoverflow.com/questions/13240047/making-2-select2-drop-downs-one-issuing-a-remote-query-become-mutually-exclusi) but should have come here first.

I've got one dropdown:

$remote.select2({
allowClear: true,
minimumInputLength: 2,
query: function(options){
$.ajax({
dataType: 'json',
url: myURL + options.term,
error: function(jqXHR, textStatus, errorThrown){
smoke.alert(textStatus + ": server returned error on parsing arguments starting with " + options.term);
},
success: function(data, textStatus, jqXHR){
var results = [];
for(var i = 0; i < data.length; ++i){
results.push({id: data[i].id, text: data[i].name});
}

options.callback({results: results, more: false});
}
});
}
});

with an "input" HTML tag. Once I've selected an item from the drop down list, I'd like to be able at a later date to clear the drop down. I keep getting "Uncaught Error: cannot call val() if initSelection() is not defined." Any way to clear the box?

Igor Vaynberg

unread,
Nov 6, 2012, 12:27:15 PM11/6/12
to sel...@googlegroups.com
have you tried $remote.select2("data",null) ?

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


jon...@gmail.com

unread,
Feb 27, 2013, 2:09:44 AM2/27/13
to sel...@googlegroups.com
I'm having this problem as well. I've tried clearing the values in a select2 by the following ways:

$ele.select2('data', null);
# Error: Uncaught TypeError: Cannot read property 'id' of undefined

$ele.select2('data', {id: null})
# cannot call val() if initSelection() is not defined

Any ideas why this is happening? I am also using select2 on an input field.

Igor Vaynberg

unread,
Feb 27, 2013, 2:19:35 AM2/27/13
to sel...@googlegroups.com
jsfiddle please

-igor
> 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?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Message has been deleted

anton....@gmail.com

unread,
Mar 25, 2013, 6:06:04 AM3/25/13
to sel...@googlegroups.com
Hello, Igor.
I have the same problem for clearing my select2 field.
This is my code in jsfiddle: http://jsfiddle.net/kVkR3/2/
Have you any ideas about what am I doing wrong?

Thank you.
BR

Igor Vaynberg

unread,
Mar 25, 2013, 11:31:16 AM3/25/13
to sel...@googlegroups.com
this works fine for me in chrome...

http://jsfiddle.net/kVkR3/3/
Reply all
Reply to author
Forward
0 new messages