Solved

changing add array values to option to jquery

Posted on 2013-11-26
2
375 Views
Last Modified: 2013-12-12
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
Comment
Question by:rivkamak
2 Comments
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 250 total points
ID: 39678129
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
 
LVL 15

Accepted Solution

by:
Insoftservice earned 250 total points
ID: 39686760
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now