Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  43] [ 3]  / answers: 1 / hits: 100702  / 11 Years ago, tue, april 30, 2013, 12:00:00

I'm working with the awesome select2 control.



I'm trying to clean and disable the select2 with the content too so I do this:



$(#select2id).empty();
$(#select2id).select2(disable);


Ok, it works, but if i had a value selected all the items are removed, the control is disabled, but the selected value is still displayed. I want to clear all content so the placeholder would be showed. Here is a example I did where you can see the issue: http://jsfiddle.net/BSEXM/



HTML:



<select id=sel data-placeholder=This is my placeholder>
<option></option>
<option value=a>hello</option>
<option value=b>all</option>
<option value=c>stack</option>
<option value=c>overflow</option>
</select>
<br>
<button id=pres>Disable and clear</button>
<button id=ena>Enable</button>


Code:



$(document).ready(function () {
$(#sel).select2();

$(#pres).click(function () {
$(#sel).empty();
$(#sel).select2(disable);
});

$(#ena).click(function () {
$(#sel).select2(enable);
});
});


CSS:



#sel {
margin: 20px;
}


Do you have any idea or advice to this?


More From » jquery

 Answers
8

Why all this trouble???



use:



 $('#sel').select2('data', null);

[#78498] Monday, April 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;