1

So I have this string: 93, 94 which are the references of the values of a select.

I'm trying to set these values using:

let values = "93, 94";
$('#property-categories').selectpicker('val', values);

but not working, none of the value is selected in the select control.

If I write: $('#property-categories').selectpicker('val', [93,94]);

works, any idea?

2 Answers 2

2

To turn a comma delimited string in to an array, which is the type required here, use split().

let values = "93, 94";
$('#property-categories').selectpicker('val', values.split(', '));
Sign up to request clarification or add additional context in comments.

6 Comments

please test your code before posting.link
@jishansiddique It already works. jsfiddle.net/g4y02nfa
Because you used a very outdated version of selectpicker, for some reason
@RoryMcCrossan could I ask a little more help? The issue happen again if I provide non numerical values, eg: modern,historic,seaside,minimalist, could you please take a look at it?
Text values work too. The difference with that string is that you're using just , to delimit the string instead of , (note the trailing space after the comma). Fix that and it works fine jsfiddle.net/rgkmwd9v.
|
1

Please check this one for simple select control

var values="93,94";
$.each(values.split(","), function(i,e){
    $("#strings option[value='" + e + "']").prop("selected", true);
});

For selectpicker you need to set below code

$('.selectpicker').selectpicker();
$('.selectpicker').selectpicker('val', ['93','94']);

Check here working demo - > Click here

3 Comments

Your first example won't update the selectpicker UI and the second calls selectpicker() twice for no reason.
Also please don't plagiarise other answers
Yes, I understood the problem thanks for sharing your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.