Tab (and comma?) add two entries, one incomplete

Jake Goulding's Avatar

Jake Goulding

30 Oct, 2010 03:37 PM

Using tab to select an item doesn't quite work. If you type fo and the selected item is food, then both fo and food are added to the list. It looks like there is a missing break and/or the call to add_selected_item() should be replaced with active.click(). I fixed this locally by commenting out the add_selected_item() call and allowing the case to fall through. Thanks for the useful plugin!

  1. 1 Posted by dudeinco on 08 Dec, 2010 06:09 PM

    dudeinco's Avatar

    You're my new hero!!!!

    This fix also ONLY allows items in the select list to be selected as well (which was what I was looking for).

  2. 2 Posted by James Moberg on 30 Dec, 2010 10:42 PM

    James Moberg's Avatar

    Is there a way to add this as an option? I have multiple autoSuggest fields and some are used as ajax lookups where I would only want items allowed to be selected (not entered on-the-fly since they don't have an paired ID/Value.) Other fields are free-form "Tag" fields and allow user entry as well as choosing from an ajax request.

    Is this possible without having to fork the plugin into 2 different versions?

  3. 3 Posted by Xnuiem on 09 Feb, 2011 09:31 PM

    Xnuiem's Avatar

    James,

    Yes.

    I called it "noFree" as in no free text allowed.

    Change line 193 from

    add_selected_item(n_data, "00"+(lis+1));

    to

    if (false == opts.noFree){ add_selected_item(n_data, "00"+(lis+1)); }

    Then, up in the top, around line 51, add

    noFree: false

    as the final option. Make sure to put a comma after the one before it. Mine looks like:

            resultClick: function(data){},
            resultsComplete: function(){},
            noFree: false
        };  
        var opts = $.extend(defaults, options);
    

    There you go. Now pass it "noFree: true" in the options if you want to disable free form text.

  4. 4 Posted by James Moberg on 09 Feb, 2011 10:25 PM

    James Moberg's Avatar

    Thanks... that's exactly what I was looking for. (I was actually hoping it was already built-in and that I just couldn't find it, but this is just as good.) Hopefully something like this will be added in future versions.

  5. 5 Posted by Jack Alexander on 29 Mar, 2011 06:27 AM

    Jack Alexander's Avatar

    (I THINK I FOUND A SOLUTION) Hi guys,
    In my case, I was still having a problem with this bug because unlike others, I need the users to be able to tab/comma for items on the fly AND also be able to tab/comma items that have been selected from the result list, ALL in the same autosuggest box.
    I'm a newbie when it comes to jquery and this autosuggest plugin so please correct me if I made a mistake. ..but this is how I fixed it...
    In the tab/comma case, I added an if-statement to make sure that there are zero elements that have the "active" class, and I wrapped it around lines 192-193. I then also added 2 more lines in that if-statement so that the suggestion box would get cleared.....My final code looks like this (starting from line 190):

    n_data[opts.selectedItemProp] = i_input;
    n_data[opts.selectedValuesProp] = i_input;\n
    if ($(".active",results_holder).length == 0){
        var lis = $("li", selections_holder).length;
        add_selected_item(n_data, "00"+(lis+1));
        if (timeout){ clearTimeout(timeout); }
        timeout = setTimeout(function(){ keyChange(); }, opts.keyDelay);
    }
    input.val("");
    

    Hope this helps

  6. 6 Posted by Jack Alexander on 29 Mar, 2011 06:30 AM

    Jack Alexander's Avatar

    (Ignore that new-line character that is at the end of the second line of code in my previous post....I'm not sure how it got there)

  7. 7 Posted by Matias Larsson on 13 Jun, 2011 04:39 PM

    Matias Larsson's Avatar

    And if anyone wants both @Xnuiems fix as well as a fix for the incomplete entry you can change line 193 to;

    if (false == opts.noFree && active.length == 0){ add_selected_item(n_data, "00"+(lis+1)); }

    as well as move row 199: var active = $("li.active:first", results_holder); to somewhere before the switch(e.keyCode) row.

    It would be nice if these fixes could be added to a new release.

  8. 8 Posted by ed.estes on 27 Jun, 2011 01:44 PM

    ed.estes's Avatar

    Glad to have found this thread. I just asked about this in another similar thread but luckily found the solution here.

    Is Drew not working on this anymore? (it's been a while since there has been any code updates and this is a bug I would think would get fixed along the way)

    Thanks!

  9. 9 Posted by Paolo on 09 Aug, 2011 10:19 AM

    Paolo's Avatar

    Great stuff James - thanks for the code. Works like a bomb!

  10. 10 Posted by pjack on 04 Jan, 2012 07:40 AM

    pjack's Avatar

    This was a major issue for me and an easy fix. Thanks so much for sharing.

  11. 11 Posted by Manish on 19 May, 2012 06:58 AM

    Manish's Avatar

    Hello every one,

    i need if user
    copy and past user email at text box like if

    [email blocked],[email blocked],[email blocked],[email blocked]

    if past that data at text box then add 4 different item at list

  12. 12 Posted by Tridem on 02 Jul, 2012 11:28 AM

    Tridem's Avatar

    I successfully implemented Xnuiem "noFree" option and applied the code changes suggested by Matias Larsson - works like a charm! Thanks!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac