Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks to the answers I received, I was able to create something like the following, which suits my needs. My question was somewhat ambiguous. Thanks for following up. My final problem was solved by including "selected" in the option that I wanted selected.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() { $('#mySelect').children().remove().end().append('&lt;option selected value="One"&gt;One option&lt;/option&gt;') ; // clear the select box, then add one option which is selected $("input[name='myRadio']").filter( "[value='1']" ).attr( "checked", "checked" ); // select radio button with value 1 // Bind click event to each radio button. $("input[name='myRadio']").bind("click", function() { switch(this.value) { case "1": $('#mySelect').find('option').remove().end().append('&lt;option selected value="One"&gt;One option&lt;/option&gt;') ; break ; case "2": $('#mySelect').find('option').remove() ; var items = ["Item1", "Item2", "Item3"] ; // Set locally for demo var options = '' ; for (var i = 0; i &lt; items.length; i++) { if (i==0) { options += '&lt;option selected value="' + items[i] + '"&gt;' + items[i] + '&lt;/option&gt;'; } else { options += '&lt;option value="' + items[i] + '"&gt;' + items[i] + '&lt;/option&gt;'; } } $('#mySelect').html(options); // Populate select box with array break ; } // Switch end } // Bind function end ); // bind end }); // Event listener end</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;label&gt;One&lt;input name="myRadio" type="radio" value="1" /&gt;&lt;/label&gt; &lt;label&gt;Two&lt;input name="myRadio" type="radio" value="2" /&gt;&lt;/label&gt; &lt;select id="mySelect" size="9"&gt;&lt;/select&gt;</code></pre> </div> </div> </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