Solved

changing add array values to option to jquery

Posted on 2013-11-26
2
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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