-1

enter image description here

I get these type of array in my javascript function as shown in image.Now i want selected all city in my dropdownlist which i get in my javascript array. I have tried it but i didn't get success.How to do it.

function searchitem(id,name)
{
 var ADDRESS = value.Address;
 console.log(ADDRESS);
 $("#Address option[value='" + vin + "']").prop("selected", true);
}

HTML Code

<select class="ct-js-select ct-select-lg" multiple="multiple" id="Address" name="Address[]" style="margin-left:14px;">
    <option value="any">Any</option>
    <optgroup label="City">
    <?php foreach($Locations_Response->city as $CITIES){ ?>
        <option value="<?php echo  $CITIES; ?>"><?php echo  $CITIES; ?></option>
    <?php } ?>
    </optgroup> 
</select>
empiric
  • 7,825
  • 7
  • 37
  • 48
Code ninja
  • 37
  • 1
  • 8

2 Answers2

1

You need a loop:

var ADDRESS = ['Aberdeen Twp.','2'];

$.each(ADDRESS,function(i,vin) {
       $("#Address option[value='" + vin + "']").prop("selected", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="ct-js-select ct-select-lg" multiple="multiple" id="Address" name="Address[]" style="margin-left: 14px; " tabindex="-1"> 
<option value="any">Any</option>
<optgroup label="City"> 
  <option value="Aberdeen Twp.">Aberdeen Twp.</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</optgroup>
</select>
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0
<select id='slc' name='slc'>
<?php
for($c=0;$c<10;$c++)
{
    echo"<option> ".$c." </option>";    
}
?>

script here

var txt=$('#slc').find(":selected").text();
J.WCT
  • 92
  • 9