I want to pre-select multiple values from the below dropdown select. For this example i want to select 'Abbey' & 'Belgrave'
<select id="dropdown" multiple>
<option value='Belgrave'>Belgrave</option>
<option value='Abbey'>Abbey</option>
<option value='Castle'>Castle</option>
</select>
I can achieve this manually which works fine like this:
$('#dropdown').selectpicker('val', ['Abbey', 'Belgrave']);
The problem is by passing the array in string it does not work:
var wards = dsFilterOptions.Wards.split(",");
var strWard = "[";
for (i = 0; i < wards.length; i++) {
strWard += "'" + wards[i] + "',";
}
strWard = strWard.slice(0, -1); //remove last ma
strWard += "]";
$('#dropdown').selectpicker('val', strWard);
The above does not select the Abbey & Belgrave values.What i can do.
I want to pre-select multiple values from the below dropdown select. For this example i want to select 'Abbey' & 'Belgrave'
<select id="dropdown" multiple>
<option value='Belgrave'>Belgrave</option>
<option value='Abbey'>Abbey</option>
<option value='Castle'>Castle</option>
</select>
I can achieve this manually which works fine like this:
$('#dropdown').selectpicker('val', ['Abbey', 'Belgrave']);
The problem is by passing the array in string it does not work:
var wards = dsFilterOptions.Wards.split(",");
var strWard = "[";
for (i = 0; i < wards.length; i++) {
strWard += "'" + wards[i] + "',";
}
strWard = strWard.slice(0, -1); //remove last ma
strWard += "]";
$('#dropdown').selectpicker('val', strWard);
The above does not select the Abbey & Belgrave values.What i can do.
Share Improve this question edited Aug 26, 2014 at 16:41 user2906420 asked Aug 26, 2014 at 16:31 user2906420user2906420 1,2696 gold badges31 silver badges44 bronze badges 5-
Since you're trying to set 2 values, I presume it is a multiselect. You should add
multiple
to the<select>
– Stryner Commented Aug 26, 2014 at 16:35 - Ninsly I already have multiple in the select :- updated above – user2906420 Commented Aug 26, 2014 at 16:41
-
Oh ok. Is there any problem with just doing
$('#dropdown').selectpicker('val', wards);
– Stryner Commented Aug 26, 2014 at 16:47 - What is the initial string you're working with? And why, after splitting it into an array, are you converting it back into a string? – David Thomas Commented Aug 26, 2014 at 16:51
- David the initial str is built from the user input and does not have the 'apostrophe' around the value hence i am going through the str and formatting it with ' ' . And directly using it does not work also. – user2906420 Commented Aug 26, 2014 at 16:57
2 Answers
Reset to default 6You could use JSON.parse
to revert the string to an array, so:
$('#dropdown').selectpicker('val', JSON.parse(strWard));
Edit, with single quotes fixed:
var wards = dsFilterOptions.Wards.split(",");
var strWard = '[';
for (i = 0; i < wards.length; i++) {
strWard += '"' + wards[i] + '",';
}
strWard = strWard.slice(0, -1); //remove last ma
strWard += ']';
$('#dropdown').selectpicker('val', JSON.parse(strWard));
response.user_accounts
is an array, but it just shows the first value as a dropdown selection, not all values of the array. Please suggest
if (response.user_accounts) {
$('#account').selectpicker('val', response.user_accounts.account_id);
}