Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind <select> from list of comma separated values in JQuery?

How we can bind select tag with variable data?..

Here is my sample code here.

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script>
var google.language.Languages = {
  'AFRIKAANS' : 'af',
  'ALBANIAN' : 'sq',
  'AMHARIC' : 'am',
  'ARABIC' : 'ar',
  'ARMENIAN' : 'hy',
  'AZERBAIJANI' : 'az' ...
}
</script>

I want Bind my select tag using above languages

<select id="langsel" name="selector">
        <option value="">Select a language</option>
</select>

How to bind "langsel" select with above variable.?

I need output like below :

 <select id="langsel" name="selector">
            <option value="">Select a language</option>
            <option value="af">AFRIKAANS</option> 
            ....
    </select>
like image 230
Abhishek B. Avatar asked Aug 19 '11 06:08

Abhishek B.


1 Answers

$(function() {

    var languages = {
      'AFRIKAANS' : 'af',
      'ALBANIAN' : 'sq',
      'AMHARIC' : 'am',
      'ARABIC' : 'ar',
      'ARMENIAN' : 'hy',
      'AZERBAIJANI' : 'az'
    }

    var sel = $('#langsel');

    $.each(languages, function(key, value) { 
        sel.append('<option value="'+value+'">'+key+'</option>');
    });

});

Working Fiddle: http://jsfiddle.net/qtb6S/

like image 130
daryl Avatar answered Sep 23 '22 21:09

daryl