Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're not grabbing the values from the object correctly.</p> <p>The format is like so:</p> <pre><code>{key1:[{0, 1}], key2:[{0, 1}], key3:[{0, 1}]} </code></pre> <p>When you use <code>$.each</code>, it selects each one in this state:</p> <pre><code>[{0, 1}] </code></pre> <p>On the first iteration, it would select the value associated with key1, on the second, key2, etc.</p> <p>Since you have arrays nested in each key's value, you need to specify that it locate the first array in the matched value:</p> <pre><code>var _loc = { ALL_BUYS: [{ Text: "All Buys", Value: "AAA"}], NETWORK_BREAK: [{ Text: "Network Break", Value: "NTN"}], SYNDICATED_BREAK: [{ Text: "Syndicated Break", Value: "STA"}], LOCAL_BREAK: [{ Text: "Local Break", Value: "LTA"}], NATIONAL_LOCAL: [{ Text: "National/Local", Value: "LTN"}], LOCAL_LOCAL: [{ Text: "Local/Local", Value: "LTL"}] }; lstOptions = $("select"); $.each(_loc, function(i, item){ $("&lt;option&gt;&lt;/option&gt;") .appendTo(lstOptions) .html(item[0].Text) .attr("value", item[0].Value); });​ </code></pre> <p><a href="http://jsfiddle.net/purmou/33HPh/1/" rel="nofollow">Demo</a>.</p> <p>Note the <code>item[0]</code>. What this does is select the array with the index <code>0</code> in the value that it grabbed. Since there is only one, that single array's index is <code>0</code>, so it's selecting that specific one. Then you find and set its text and value.</p> <p>Also note that I changed <code>val()</code> to <code>attr()</code>, because <code>option</code> elements don't support <code>val()</code> (you could, however, use it on a <code>select</code> element to set the option whose value is equal to the one specified in this function to make it selected).</p> <p><strong>TIP:</strong> You could also get rid of the literal array indicators around each nested object and use the original method. <a href="http://jsfiddle.net/purmou/33HPh/2/" rel="nofollow">Demo</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload