Monday, May 30, 2011

Re: [phpXperts] Need help for js array

 

Hello Rahat Bashir,


Adjust your array elements like below and try the following code:

<script type="text/javascript">
$(document).ready(function(){

    var mylist = new Array(5);
      for (var i = 0; i < mylist.length; i++) {
        mylist[i] = new Array(2);
      }
     
        mylist[0][0] = 'value1';
        mylist[0][1] = 'value11';
      
        mylist[1][0] = 'value2';
        mylist[1][1] = 'value22';
      
        mylist[2][0] = 'value3';
        mylist[2][1] = 'value33';
      
        mylist[3][0] = 'value4';
        mylist[3][1] = 'value44';
      
        mylist[4][0] = 'value5';
        mylist[4][1] = 'value55';
   
    $.each(mylist,function( index, objVal ){
     
        $('select[name="myselect"]').append($('<option></option>').val(objVal[0]).html(objVal[1]));
   
    });

});

</script>

Make sure that you have a dropdownlist element with name="myselect" inside your html body tag.

Like:

<select name="myselect" ></select>

Good Luck.



-Mahmudul



From: Rahat Bashir <rahat.bashir@gmail.com>
To: phpexperts@yahoogroups.com
Sent: Mon, May 30, 2011 4:23:25 PM
Subject: [phpXperts] Need help for js array

 

Hello Experts,

I am a new user of jquery. Can anyone help me solving the following:

I have the following javascript array:

var mylist = new Array();
    mylist[0][0] = 'value1';
    mylist[0][1] = 'value11';
   
    mylist[1][0] = 'value2';
    mylist[1][1] = 'value22';
   
    mylist[2][0] = 'value3';
    mylist[2][1] = 'value33';
   
    mylist[3][0] = 'value4';
    mylist[3][1] = 'value44';
   
    mylist[4][0] = 'value5';
    mylist[4][1] = 'value55';

I want to populate options of a select element with these values with jquery, like below:

<select name="myselect">
  <option value="value1">value11</option>
  <option value="value2">value22</option>
  <option>...........
</select>

I tried $.each(), but could not make it.


Regards,
Rahat Bashir

__._,_.___
Recent Activity:
Visit phpXperts website at www.phpxperts.net
MARKETPLACE

Stay on top of your group activity without leaving the page you're on - Get the Yahoo! Toolbar now.


Get great advice about dogs and cats. Visit the Dog & Cat Answers Center.

.

__,_._,___

No comments: