Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom function to limit selections is not working
    primarykey
    data
    text
    <p>I have a html form having 7 songs with checkboxes, Users can only select up to 5 songs. I created a js function to do this. It works nice if I go selecting songs and prevent selecting 6th song and show alert message. But once I have selected 5 songs and try to deselect any song, it also prevent we to do so and show the same alert.</p> <p>here is my HTML markup (sorry for the table layout this is my client's choice, he only want table even if I can do the same without the table)</p> <pre><code> &lt;form id="apc-form" dir="rtl" method="post" action="ap-contact-form.php"&gt; &lt;table id="apc-table"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 1&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/i&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 2&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 4&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 5&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 6&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="song-checkbox"&gt;&lt;input type="checkbox" class="song-select"&gt;&lt;/td&gt; &lt;td class="song-icon"&gt;&lt;/td&gt; &lt;td class="song-name"&gt;Song 7&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;button id="submit" type="submit"&gt;שלח&lt;/button&gt; &lt;/form&gt; </code></pre> <p>and JQuery codes </p> <pre><code>var apcSongs = { init: function() { var sCheckbox = $('#apc-table .song-select'); var form = $('#apc-form'); form.submit(function(e) { apcSongs.validateMin(); }); sCheckbox.click(function(e) { var selectedSongs = $('#apc-table').find('tr.selected'); console.log(selectedSongs.length); if (selectedSongs.length &lt; 5) { $(this).parent().parent().toggleClass('selected'); } else { alert('You can\'t choose more than 5 songs'); return false } }); }, validateMin: function() { var selectedSongs = $('#apc-table').find('tr.selected'); if (selectedSongs.length == 0) { alert('You must select at least 1 song'); return false } } } apcSongs.init(); </code></pre> <p>You can check this <a href="http://jsbin.com/olAKiC/1/" rel="nofollow">jsbin</a></p> <p>Please check my code and tell me what can I do with my codes so that it doesn't show alert on deselecting. If you know a batter way to do this please tell me.</p>
    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.
    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