Loading ...
Sorry, an error occurred while loading the content.

Need help for js array

Expand Messages
  • Rahat Bashir
    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();
    Message 1 of 5 , May 30, 2011
    View Source
    • 0 Attachment
      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

    • MD. MUTAHERUL ISLAM
      try this : -- var mylist[0] = new Array(2);     mylist[0][0] = value1 ;     mylist[0][1] = value11 ;          mylist[1] = new Array(2);    
      Message 2 of 5 , May 30, 2011
      View Source
      • 0 Attachment
        try this : -->

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





        Regards,
         
        Md. Mutaherul Islam .( Babu )





        From: Rahat Bashir <rahat.bashir@...>
        To: phpexperts@yahoogroups.com
        Sent: Monday, May 30, 2011 4:23 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


      • Md. Mahmudul Hasan
        Hello Rahat Bashir, Adjust your array elements like below and try the following code: $(document).ready(function(){ var mylist
        Message 3 of 5 , May 30, 2011
        View Source
        • 0 Attachment
          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@...>
          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

        • Anjan Bhowmik
          Examine the following code, you will know how to do it.
          Message 4 of 5 , May 30, 2011
          View Source
          • 0 Attachment
            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@...> 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@...info@...
            Site: http://www.onlyfreelancer.com/ - Only for Freelancer's!

          • Rahat Bashir
            Special Thanks to Mahmudul and Md. Mutaherul Islam ( Babu ) The reply was quick and the solution worked for me very well. Regards, Rahat Bashir
            Message 5 of 5 , May 30, 2011
            View Source
            • 0 Attachment
              Special Thanks to Mahmudul and Md. Mutaherul Islam ( Babu )

              The reply was quick and the solution worked for me very well.

              Regards,
              Rahat Bashir




              On Mon, May 30, 2011 at 8:28 PM, Md. Mahmudul Hasan <mukul_cse_ruet@...> 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


            Your message has been successfully submitted and would be delivered to recipients shortly.