summary summary - 2 months ago 14
jQuery Question

how to convert tapestry arraylist into array in jquery

I am working on an application that uses tapestry , jquery , ajax .

I need to get a drop down to act as autocomplete drop down.
So i think of taking drop down option arraylist into an array and write autocomplete method to it .
below is existing code

<td ><span jwcid="countryList"
onChange="javascript:onChangeCountry(this);" />
</td>


Above tag converts into drop down box at run time with id = countryList.

I want to get text values of countryList into an array , how can i do it ?
I tried below method , but it only gave me first index 's text .

var carray= [];
$('#countryList :selected').each(function(i, selected){
carray[i] = $(selected).text();
});

$( "#xyz" ).autocomplete({
source: carray
});


How can i get all text values in carray ?

What am i doing wrong ?

Please help .

Thanks Much.

Answer

You can read the option like this. You can try with this code instead:

var options = [];
$(document).ready(function() {
  
  $('#myselect option').each(function(i, selected) {
    options[i] = ($(selected).text());
  });

  console.log(options);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="myselect">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>