0

I have a javascript function. This function is supposed to populate a html select-box by adding options.

Argument passed is, for example, [['val1','txt1'],['val2','txt2']]

I expected that it would generate the html as under:

<select id='element_id'>
    <option value='val1'>txt1</option>
    <option value='val2'>txt2</option>
</select>

<script>
function popu_select(x) {
    for (i in x) {
      $('#element_id').append($(document.createElement("option")).attr("value", "i[0]").text("i[1]"));
              }
</script>

But it is not populating the select-box. What I may be doing wrong here?

Regards, Vineet

2
  • 1
    It's worth noting that iterating through an array with "for ... in" is considered bad practice. Instead, it's better to use a numeric index in a plain "for" loop. Commented Nov 28, 2011 at 16:32
  • @Pointy, You are right. I will make use of a numeric index in a plain "for" loop. Thanks. Commented Nov 28, 2011 at 17:15

4 Answers 4

4

At least, you need to get the item of the array (i is only the index), and remove quotes when accessing the variables. Try this:

  function popu_select(x) {
    for (i in x) {
      var item = x[i]
      $('#element_id').append($(document.createElement("option")).attr("value", item[0]).text(item[1]));
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Should also be noted that appending each time through the loop will be a performance hit when your list gets longer. I recommend storing your options in a variable and appending once the loop is complete.
for..in for arrays kills more kittens than eval :-(
Thanks to all. I will try each method suggested here & adopt the best suited to my purpose. If stuck-up, will pot the problem here again.
@idrumgood, sometimes I do have lists as long as thousands of items. But I did not understand what you suggest by "I recommend storing your options in a variable and appending once the loop is complete". Bcoz the options are there in a variable itself. What is meant by "appending once the loop is complete". Can you pl. explaint in a bit detail? Thanks.
@Vineet you're creating a <select> element with thousands of <option> elements inside? Won't that be somewhat difficult to use (to say the least)?
|
2

You should use JSON to pass arguments in:

var args = [{value:1, text:'one'}, {value:2, text:'two'}];

$.each(para, function(i, option){ 
  $('#element_id').append("<option value='" + option.value + "'>" + option.text + "</option>");
});

1 Comment

Thanks. I will try this method suggested here & if stuck-up, will pot the problem here again.
1

I have updated your code here so that it works.

http://jsfiddle.net/52nRL/

using the jQuery $.each function.

Comments

1

Not jQuery, but this example does exactly what you want. It even includes an example for adding options from an array like you're doing here.

1 Comment

Thanks. I will try this method suggested here & if stuck-up, will pot the problem here again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.