Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate options in select dynamically
    primarykey
    data
    text
    <p>I have constructed a <strong>dynamic <code>select</code></strong> using <strong><code>jQuery</code></strong> like below:</p> <p>Based on <strong><code>YesOrNo</code></strong>, I have to show/hide options in <strong><code>Source</code></strong> and <strong><code>Type</code></strong></p> <pre><code>Yes/No Source Type --------------------------------------------- Yes Yes1 and Yes2 Yes No No1 and No2 No1 to 6 </code></pre> <p><strong>HTML:</strong></p> <pre><code>&lt;select id="YesOrNo" name='YesOrNo'&gt; &lt;option value=''&gt;Select Yes/No&lt;/option&gt; &lt;option value='1'&gt;Yes&lt;/option&gt; &lt;option value='2'&gt;No&lt;/option&gt; &lt;/select&gt; &lt;select id='Source' name='Source'&gt; &lt;option value=''&gt;Select Source&lt;/option&gt; &lt;option data-aob='Yes' value='1'&gt;Yes1&lt;/option&gt; &lt;option data-aob='Yes' value='2'&gt;Yes2&lt;/option&gt; &lt;option data-aob='No' value='3'&gt;No1&lt;/option&gt; &lt;option data-aob='No' value='4'&gt;No2&lt;/option&gt; &lt;/select&gt; &lt;select id="Type" name='Type'&gt; &lt;option value=''&gt;Select Type&lt;/option&gt; &lt;option data-aob='No' value='1'&gt;No1&lt;/option&gt; &lt;option data-aob='No' value='2'&gt;No2&lt;/option&gt; &lt;option data-aob='No' value='3'&gt;No3&lt;/option&gt; &lt;option data-aob='No' value='4'&gt;No4&lt;/option&gt; &lt;option data-aob='Yes' value='5'&gt;Yes&lt;/option&gt; &lt;option data-aob='No' value='6'&gt;No5&lt;/option&gt; &lt;option data-aob='No' value='7'&gt;No6&lt;/option&gt; &lt;/select&gt; </code></pre> <p><strong>JQuery:</strong></p> <pre><code>$('#YesOrNo').on('change', function () { if (this.value === '1') { $('#Source option[data-aob=Yes]').show(); $('#Source option[data-aob=No]').hide(); $('#Type option[data-aob=Yes]').show(); $('#Type option[data-aob=No]').hide(); $("#Source option:selected").prop("selected", false); $("#Type option:selected").prop("selected", false); } else if (this.value === "2") { $('#Source option[data-aob=Yes]').hide(); $('#Source option[data-aob=No]').show(); $('#Type option[data-aob=Yes]').hide(); $('#Type option[data-aob=No]').show(); $("#Source option:selected").prop("selected", false); $("#Type option:selected").prop("selected", false); } else { $('#Source option').show(); $('#Type option').show(); $("#Source option:selected").prop("selected", false); $("#Type option:selected").prop("selected", false); } }); </code></pre> <p>Here is my <a href="http://jsfiddle.net/ubVfa/" rel="nofollow"><strong>JSFiddle</strong></a></p> <p>This is working perfect as I expected. Since the values are obtained from database, I have a problem while displaying the <strong>selected</strong> option in <code>YesOrNo</code>.</p> <pre><code>&lt;select id="YesOrNo" name='YesOrNo'&gt; &lt;option value=''&gt;Select Yes/No&lt;/option&gt; &lt;option selected value='1'&gt;Yes&lt;/option&gt; &lt;option value='2'&gt;No&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Here the option <strong>"Yes"</strong> is selected by default, but here <strong><a href="http://jsfiddle.net/ubVfa/1/" rel="nofollow">http://jsfiddle.net/ubVfa/1/</a></strong> still showing the four options and <strong><code>Type</code></strong> showing all 7 options. </p> <pre><code>Yes/No Source Type -------------------------------------------------------------------- Yes(SELECTED by default) All 4 options All 7 Options </code></pre> <p>How to tune my code to update the options in <code>select</code> dynamically.</p> <p>Any help is appreciated. </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.
 

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