Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I cycle through HTML select <option> values on click?
    primarykey
    data
    text
    <p>In a calendar app, I have a simple select that has option tag with values 0-11 for the corresponding months. I can cycle through the months one-way (from Dec to Jan), but I can't get it to work going the other way. It only shows Feb and Dec.</p> <p>Here's <a href="http://jsfiddle.net/mtJRn/19/" rel="nofollow">a jsfiddle</a> showing what I've already done.</p> <p>Here's the HTML:</p> <pre><code>&lt;select id="MonthName" class="form-control"&gt; &lt;option value="0"&gt;January&lt;/option&gt; &lt;option value="1"&gt;February&lt;/option&gt; &lt;option value="2"&gt;March&lt;/option&gt; &lt;option value="3"&gt;April&lt;/option&gt; &lt;option value="4"&gt;May&lt;/option&gt; &lt;!-- etc, through 11 --&gt; &lt;/select&gt; &lt;!-- 'Buttons' --&gt; &lt;span class="chevron chevronLeft floatLeft"&gt;◀&lt;/span&gt; &lt;span class="theMonth"&gt;Month&lt;/span&gt; &lt;span class="chevron chevronRight floatRight"&gt;▶&lt;/span&gt; </code></pre> <p>Here's the jQuery:</p> <pre><code>$('.chevron').on('click', function() { // This if works perfectly, and will reset to 11 once the selectedMonth is 0 if ($(this).hasClass('chevronLeft')) { var selectedMonth = $('#MonthName').val(); (selectedMonth != 0) ? $('#MonthName').val(selectedMonth - 1).trigger('change') : $('#MonthName').val(11).trigger('change'); } else { // This doesn't work, I tried declaring the variable locally, // different less/greater than expressions, but nothing works var selectedMonth = $('#MonthName').val(); (selectedMonth !== 11) ? $('#MonthName').val(selectedMonth + 1).trigger('change') : $('#MonthName').val(0).trigger('change'); } }); $('#MonthName').on('change', function() { var monthText = $('#MonthName option:selected').text(); $('.theMonth').text(monthText); }); </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. 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