I’m going to filter some countries from <select id="country" ...> according their value. I’ve tried grep and filter in jQuery but not successful. Just for one element it’s working ok.
I don’t know how to make correct array in jQuery.
jQuery.noConflict();
function change_country(seldd) {
var look_for_id=''; var opt_id=[];
jQuery('#countries').html("");
jQuery("#countries").append("<option value='0'>-Country-</option>");
if(seldd.value=='0') { look_for_id='N'; }
if(seldd.value=='1'){ look_for_id='Y'; opt_id=jQuery.filter($("#country option").val(["1, 2, 3, 4"])); }
if(seldd.value=='2'){ look_for_id='Y'; opt_id=jQuery.grep(['5', '6', '7', '8']); }
if(look_for_id=='Y') {
jQuery("#country option[value='"+opt_id+"']").each(function() {
jQuery("#countries").append("<option value='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</option>");
});
} else {
jQuery("#country option").each(function() {
jQuery("#countries").append("<option value='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</option>");
});
}
}
<div>
<select id="region" id="srv_type" onchange="change_fruit(this)" >
<option value="0" selected="selected">Регион</option>
<option value="1">Asia</option>
<option value="2">Europe</option>
</select> </div>
<div style="display:none;" >
<select id="country" name="country" class="inputbox">
<option value="" selected="selected">Страна</option>
<option value="1" >Indonesia</option>
<option value="2">Thailand</option>
<option value="3">Singapore</option>
<option value="4">Malaysia</option>
<option value="5">Greece</option>
<option value="6">Italy</option>
<option value="7">Monaco</option>
<option value="8">Spain</option>
</select>
</div>
<select name="countries" id="countries">
<option value="0">-Country-</option>
</select>
a jquery array is the same as a javascript array.e.g.
or
If you add things to your array e.g.
you can the loop though them using jquery .each or a for loop. You could also have an array of arrays e.g.
then loop through the main array and access the other elements via their index.
thing[0][0] would output UK.
Just did a quick google and this might help you:
http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
It shows some of the other things you can do with arrays