Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

changing add array values to option to jquery

I have an old javascript script with a list of an array, and then this script to put the array into the specific select

   for(ctr=0;ctr<Array_Models[num].length;ctr++)
   {
      document.form1.ccf_state.options[ctr]=new Option(Array_Models[num][ctr],Array_Models[num][ctr]);
   }
   

   //Set the length of the select list
   document.form1.ccf_state.length=Array_Models[num].length;

Open in new window


Now I need to switch it over to jquery. I have 2 select boxes on the page that need to be changed and I need to do an each function.
How would I switch the above array to work with this new jquery?

$.each(selectValues, function(key, value) {   
     $('#mySelect')
         .append($("<option></option>")
         .attr("value",key)
         .text(value)); 
});

Open in new window

0
rivkamak
Asked:
rivkamak
2 Solutions
 
Rainer JeschorCommented:
Hi,
according to your original script, it seems that the value and the text are always the same.

Therefore the following should work:
var selectValues = Array_Models[num];
$.each(selectValues, function( index, value ) {
  $('#mySelect')
         .append($("<option></option>")
         .attr("value",value)
         .text(value)); 
});

Open in new window


Sample:
http://jsfiddle.net/EE_RainerJ/V8Lkf/

HTH
Rainer
0
 
InsoftserviceCommented:
Please copy paste the code and save it as "test.html" and try out

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>Love to marry is an free matrimonial website </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="matrimonial, free sms, tourist,hosting">
  <meta name="Description" content="get free advice for your wedding,tourist and holidays package available">
  <script type="text/javascript" src="http://www.lovetomarry.com/media/scripts/jquery-1.4.2.js?v=1"></script>

  <script type="text/javascript">
  $(document).ready(function() {
   var numbers = { "1": "test 1", "2": "test 2" };
        $.each(numbers, function(val, text) {

            $('#items').append(
                $('<option></option>').val(val).html(text)
            );  
			});});
  </script>
 </head>

 <body>
  <select name='items' id='items'>
  </select>
 </body>
</html>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now