Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add value from select option with array
    primarykey
    data
    text
    <p>I have create bellow code and now i want to add value inside select option. Curently i am not getting value in all there drop downbox. For example you can set your option value to help me.</p> <p>I have created array like this way:</p> <pre><code>var d = new Array("Any","D01 Boat Quay / Raffles Place","D02 Chinatown / Tanjong Pagar","D03 Alexandra / Commonwealth","D04 Harbourfront / Telok Blangah"); var options = ''; for (var i = 0; i &lt; d.length; i++) { options += '&lt;option&gt;' + d[i] + '&lt;/option&gt;'; } $("#TypeList1").html(options); sb2.sync(); </code></pre> <p>I have tried with this code:</p> <pre><code>var dd = { Any : 'Any', 1 : 'D01 Boat Quay / Raffles Place', 2 : 'D02 Chinatown / Tanjong Pagar', 36 : 'D03 Alexandra / Commonwealth', 37 : 'D04 Harbourfront / Telok Blangah', 38 : 'D05 Buona Vista / West Coast' }; var select = document.getElementById("TypeList1"); for(index in dd) { select.options[select.options.length] = new Option(dd[index], index); } </code></pre> <p><strong>MY CODE:</strong></p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Custom Styled Selectbox&lt;/title&gt; &lt;link rel="stylesheet" href="http://www.roblaplaca.com/docs/custom-selectbox/css/customSelectBox.css" /&gt; &lt;!--&lt;link rel="stylesheet" href="customSelectBox.css" /&gt;--&gt; &lt;/head&gt; &lt;body class="noJS"&gt; &lt;script type="text/javascript"&gt; try{Typekit.load();}catch(e){} var bodyTag = document.getElementsByTagName("body")[0]; bodyTag.className = bodyTag.className.replace("noJS", "hasJS"); &lt;/script&gt; &lt;style type="text/css"&gt; .hasJS select.custom1 { position: absolute; left: -999em; } &lt;/style&gt; &lt;div class="grid-system clearfix"&gt; &lt;div class="row"&gt; &lt;div class="col span-9"&gt; &lt;div class="example clearfix"&gt; &lt;select class="custom interactive" id="properties"&gt; &lt;!--&lt;option value="selectone"&gt;Select a Type&lt;/option&gt;--&gt; &lt;option value="One" selected&gt;One&lt;/option&gt; &lt;option value="Two"&gt;Two&lt;/option&gt; &lt;option value="Three"&gt;Three&lt;/option&gt; &lt;option value="Four"&gt;Four&lt;/option&gt; &lt;/select&gt; &lt;select class="custom interactive" id="TypeList"&gt; &lt;option selected&gt;Any&lt;/option&gt; &lt;option&gt;Two&lt;/option&gt; &lt;option&gt;Three&lt;/option&gt; &lt;option&gt;Four&lt;/option&gt; &lt;/select&gt; &lt;select class="custom1 interactive1" id="TypeList1"&gt; &lt;/select&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://github.com/vitch/jScrollPane/raw/master/script/jquery.jscrollpane.js"&gt;&lt;/script&gt; &lt;script src="http://www.roblaplaca.com/docs/custom-selectbox/js/SelectBox.js"&gt;&lt;/script&gt; &lt;!--&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="jquery.jscrollpane.js"&gt;&lt;/script&gt; &lt;script src="SelectBox.js"&gt;&lt;/script&gt; --&gt; &lt;script type="text/javascript"&gt; $(function() { window.prettyPrint &amp;&amp; prettyPrint() /* This is how initialization normally looks. Typically it's just $("select.custom"), but to make this example more clear I'm breaking from the pattern and excluding interactive */ var sb, sb2; $("select.custom").not(".interactive").each(function() { sb = new SelectBox({ selectbox: $(this), height: 150, width: 200 }); }); $("select.custom1").not(".interactive").each(function() { sb2 = new SelectBox({ selectbox: $(this), height: 150, width: 250 }); }); /* Adding some extra functionality for "interactive" selects */ var TypeList = { //selectone: ["Any"], 'One': ["Any", "Landed", "Condominium", "HDB", "Others"], 'Two': ["Any", "Landed", "Condominium", "HDB", "Others"], 'Three': ["Any", "Industrial", "Retail", "Land", "Office", "Others"], 'Four': ["Any", "Industrial", "Retail", "Land", "Office", "Others"] } var defaultSelectboxSettings = { height: 150, width: 150 }; var country_SB = null, city_SB = null; $("select.interactive").each(function() { if ($(this).attr("id") == "properties") { country_SB = new SelectBox($.extend(defaultSelectboxSettings, { selectbox: $(this), changeCallback: function(val) { if (TypeList[val]) { city_SB.enable(); updateCities(val); } if (val == "selectone") { city_SB.disable(); } &lt;!------------------------------Code By Me----------------------------&gt; var getType = jQuery( "#TypeList option:selected" ).text(); if(getType == "HDB"){ var e = new Array("Any","Boat Quay","Chinatown","Havelock Road","Marina Square"); var options = ''; for (var i = 0; i &lt; e.length; i++) { options += '&lt;option&gt;' + e[i] + '&lt;/option&gt;'; } $("#TypeList1").html(options); sb2.sync(); }else{ var d = new Array("Any","D01 Boat Quay / Raffles Place","D02 Chinatown / Tanjong Pagar","D03 Alexandra / Commonwealth","D04 Harbourfront / Telok Blangah"); var options = ''; for (var i = 0; i &lt; d.length; i++) { options += '&lt;option&gt;' + d[i] + '&lt;/option&gt;'; } $("#TypeList1").html(options); sb2.sync(); } &lt;!------------------------------Code By Me----------------------------&gt; } })); } else if ($(this).attr("id") === "TypeList") { city_SB = new SelectBox($.extend(defaultSelectboxSettings, { selectbox: $(this) })); } //if(jQuery( "#properties option:selected" ).text()) }); updateCities($("#properties").val()); if ($("#properties").val() == "selectone") { //city_SB.disable(); } &lt;!------------------------------Code By Me----------------------------&gt; if(jQuery( "#TypeList option:selected" ).text()){ var dd = new Array("Any","D01 Boat Quay / Raffles Place","D02 Chinatown / Tanjong Pagar","D03 Alexandra / Commonwealth","D04 Harbourfront / Telok Blangah"); var options = ''; for (var i = 0; i &lt; dd.length; i++) { options += '&lt;option&gt;' + dd[i] + '&lt;/option&gt;'; } $("#TypeList1").html(options); sb2.sync(); } &lt;!------------------------------Code By Me----------------------------&gt; function updateCities(val) { var $select = $("select#TypeList"), html = ""; for (var i = 0; i &lt; TypeList[val].length; i++) { html += '&lt;option value="' + TypeList[val][i] + '"&gt;' + TypeList[val][i] + '&lt;/option&gt;'; } $select.html(html); // HACK: chrome is too fast? setTimeout(function() { city_SB.sync(); }, 1); } }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any ideas or suggestions? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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