user5005768Himadree
user5005768Himadree

Reputation: 1439

Bootstrap-Multiselect: why this string of comma separated values is not working as array after conversion

I want to make array from string and then pass to multiselect to selectoptions having these values in array.

I tried the following code and it did not work:

var str =  "MAR ,APR, MAY ,JUN";
var a = (new Function("return [" +str+ "];")());
$('#example-optionClass').multiselect('select', a );

above code shows error: ReferenceError: MAR is not defined

Here is my source code:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <!-- Include the plugin's CSS and JS: -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" type="text/css"/>
<script type="text/javascript">
    $(document).ready(function() {
        $('#example-optionClass').multiselect({
            includeSelectAllOption: true, // add select all option as usual
            optionClass: function(element) {
                var value = $(element).val();
 
                if (value%2 == 0) {
                    return 'odd'; // reversed
                }
                else {
                    return 'even'; // reversed
                }
            }
        });
    });
</script>
<style type="text/css">
    #example-optionClass-container .multiselect-container li.odd {
        background: #eeeeee;
    }
    #example-optionClass-container .multiselect-all {
       background: #eeeeee;
    }
</style>
</head>
<body>
<div class="container">
  <h2>Bootstrap Multiselect Test</h2>  
  <div id="example-optionClass-container">
    <select id="example-optionClass" multiple="multiple">
     <option value="JAN">January</option>
	 <option value="FEB">February</option>
	 <option value="MAR">March</option>
	 <option value="APR">April</option>
	 <option value="MAY">May</option>
	 <option value="JUN">June</option>
	 <option value="JUL">July</option>
	 <option value="AUG">August</option>
	 <option value="SEP">September</option>
	 <option value="OCT">October</option>
	 <option value="NOV">November</option>
	 <option value="DEC">December</option>		
    </select>
</div>
</div>
</body>
</html>

please help me.thanks

Upvotes: 0

Views: 2056

Answers (2)

Zigri2612
Zigri2612

Reputation: 2310

You can use as,

var str = "MAR ,APR ,MAY ,JUN";

1.

  var a = str.split(' ,');

  $('#example-optionClass').multiselect('select', a);

2.

  var a = str.split(' ,');

  $('#example-optionClass').multiselect(a.push('select'));

Upvotes: 1

Zorken17
Zorken17

Reputation: 1896

You can always try to do it like this,

var str = "MAR, APR, MAY, JUN";

var a = (function() {
  return str.split(', ');
}());

$('#example-optionClass').multiselect('select', a);

Upvotes: 1

Related Questions