Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery logic not working
    primarykey
    data
    text
    <p>I have the following code with the logic I want as follows:</p> <ol> <li>A user can not select a sport twice. A user may have sports saved in the database and with that in mind, the database records will be loaded and displayed and the user is allowed to add additional sports as longer as they do not appear in the list. Right now I have set Baseball as coming from the database but when I select it again, my logic is allowing the user to add it again. What I am doing wrong?</li> </ol> <p>JSFIDDLE: <a href="http://jsfiddle.net/Jrqqq/" rel="nofollow">http://jsfiddle.net/Jrqqq/</a></p> <pre><code> var allVals = [{ id: 1, value: "Baseball"}]; $.each(allVals, function (k, v) { $("#results").append("&lt;label class=\"wList\"&gt;&lt;input checked=\"checked\" class=\"wList-chk\" name=\"wList[]\" type=\"checkbox\" value=\"" + v.id + "\"&gt;" + v.value + "&lt;/label&gt;"); }); $(function () { var sports = [{ id: 1, value: "Baseball" }, { id: 2, value: "Soccer" }, { id: 3, value: "Basketball" }, { id: 4, value: "Volleyball" }, { id: 5, value: "Tennis" }, { id: 6, value: "Running" }, { id: 7, value: "Swimming"}]; $("#sports").autocomplete({ source: sports, select: function (event, ui) { if ($.inArray(ui.item.id, allVals) == -1) { allVals.push(ui.item.id); $("#results") .append("&lt;label class=\"wList\"&gt;&lt;input checked=\"checked\" class=\"wList-chk\" name=\"wList[]\" type=\"checkbox\" value=\"" + ui.item.id + "\"&gt;" + ui.item.value + "&lt;/label&gt;"); $('input.wList-chk[type="checkbox"][value="' + ui.item.id + '"]').parent().effect('highlight', {}, 1500); ui.item.value = ""; } else { ui.item.value = ""; $('input.wList-chk[type="checkbox"][value="' + ui.item.id + '"]').parent().effect('highlight', {}, 1500); } } }); }); $(document).on("change", ".wList-chk", function () { if ($(this).attr('checked')) { return; } else { var thisVal = $(this).val(); var index = $.inArray(thisVal, allVals); allVals.splice(index, 1); $(this).parent('.wList').remove(); } }); </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