Monday, May 30, 2011

Re: [phpXperts] Need help for js array

 

Examine the following code, you will know how to do it.

<html>
    <head>
        <!-- Change the path to jquery as needed -->
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready
            (
                function()
                {
                    var countries = [];
                   
                    countries[0] = []; // Make sure countries[0] is an array before assigning values to it!
                   
                    countries[0][0] = 'Bangladesh';
                    countries[0][1] = 'BD';
                   
                    countries[1] = [];
                   
                    countries[1][0] = 'India';
                    countries[1][1] = 'IN';
                   
                    countries[2] = [];
                   
                    countries[2][0] = 'Pakistan';
                    countries[2][1] = 'PK';
                   
                    // Loop through the array and add items to select
                   
                    for(var i = 0; i < countries.length; i++)
                    {
                        $('#country_list').append('<option value="' + countries[i][1] + '">' + countries[i][0] + '</option>');
                    }
                   
                    // A test to see your select item text and values, to make sure they are added ok!
                   
                    $('#country_list').change
                    (
                        function()
                        {
                            alert('You seleted: "' + $(this).find('option:selected').text() + '" with value: "' + $(this).val() + '"');
                        }
                    );
                }
            );
        </script>
    </head>
    <body>
        <!--
            Be sure to assign unique ID to html element which u will access using jquery.
            Via id it is fastest to find an element.
        -->
        <select id="country_list" name="countries" size="1">
            <option value>Select Country</option>
        </select>
    </body>
</html>

On Mon, May 30, 2011 at 8:28 PM, Md. Mahmudul Hasan <mukul_cse_ruet@yahoo.com> wrote:
 

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


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




--
Anjan Bhowmik
Freelance Software & Web Developer
M : +880 - 1670 - 556419
E : anjan011@gmail.cominfo@ultrasoftbd.com
Site: http://www.onlyfreelancer.com/ - Only for Freelancer's!

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

__,_._,___

No comments: