Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just want to explain this:</p> <pre><code>if ($('.combo option:selected[value==0]')) </code></pre> <p>To check if the selected item from dropdown has value 0, you can use the following:</p> <pre><code>if ($('.combo').val() == '0') // simpler and more readable :-) </code></pre> <p>Then, it will be simpler if you already put the error message somewhere in the document. All you need to do is show/hide it. For example:</p> <pre><code>&lt;select id="dropdown-1" class="combo"&gt; &lt;option value="0"&gt;-&lt;/option&gt; &lt;option value="1"&gt;Lorem&lt;/option&gt; &lt;option value="2"&gt;Ipsum&lt;/option&gt; &lt;/select&gt; &lt;span id="error-dropdown-1" class="error" style="display: none"&gt;Please select a value!&lt;/span&gt; &lt;!-- hidden by default. can also use CSS instead of style="display: none" --&gt; &lt;input type="button" id="demo-button" value="Demo" /&gt; </code></pre> <p>And the javascript (+ jquery):</p> <pre><code>$('#demo-button').click(function() { if ($('.combo').val() == '0') { $('#error-dropdown-1').show(); } else { $('#error-dropdown-1').hide(); } }); </code></pre> <p>Here is another example. Useful if you avoid IDs (perhaps you have dynamically generated dorpdowns):</p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $('#demo-button').click(function() { $('.combo').each(function() { var div = $(this).parents('div').get(0); // get the wrapper if ($(this).val() == '0') { $(div).find('.error').show(); // show &lt;span class=".error"&gt; inside the wrapper } else { $(div).find('.error').hide(); // hide &lt;span class=".error"&gt; inside the wrapper } }); }); }); &lt;/script&gt; &lt;div&gt; &lt;!-- wrapper for 1st combo + 1st error message --&gt; &lt;select class="combo"&gt; &lt;option value="0"&gt;-&lt;/option&gt; &lt;option value="1"&gt;Lorem&lt;/option&gt; &lt;option value="2"&gt;Ipsum&lt;/option&gt; &lt;/select&gt; &lt;span class="error" style="display: none"&gt;Please select a value!&lt;/span&gt; &lt;/div&gt; &lt;div&gt; &lt;!-- wrapper for 2nd combo + 2nd error message --&gt; &lt;select class="combo"&gt; &lt;option value="0"&gt;-&lt;/option&gt; &lt;option value="1"&gt;Dolor&lt;/option&gt; &lt;option value="2"&gt;Sit&lt;/option&gt; &lt;option value="3"&gt;Amet&lt;/option&gt; &lt;/select&gt; &lt;span class="error" style="display: none"&gt;Please select a value!&lt;/span&gt; &lt;/div&gt; &lt;div&gt; &lt;!-- wrapper for 3rd combo + 3rd error message --&gt; &lt;select class="combo"&gt; &lt;option value="0"&gt;-&lt;/option&gt; &lt;option value="1"&gt;Consectetuer&lt;/option&gt; &lt;option value="2"&gt;Adipiscing&lt;/option&gt; &lt;option value="3"&gt;Elit&lt;/option&gt; &lt;/select&gt; &lt;span class="error" style="display: none"&gt;Please select a value!&lt;/span&gt; &lt;/div&gt; &lt;input type="button" id="demo-button" value="Demo" /&gt; </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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