Major bug and the solution :)

Rasmus Hansen's Avatar

Rasmus Hansen

20 Oct, 2010 12:03 AM

I noticed that I could add the first selected item twice and that the first selected value stayed in the hidden values input, even if I removed all the selected items.

After a lot of debugging I found out, that the solution was very simple:

Change line 84 from:
var values_input = $('');
To:
var values_input = $('');

And line 131
if($(this).val() == opts.startText && values_input.val() == ""){
To:
if($(this).val() == opts.startText && values_input.val() == ","){

This of course means that it now submits "," even though nothing has been selected, but at least now I can remove all values.

Hope this helps others and sorry for the bad spelling, grammar etc :)

Best regards and thanks for an amazing plugin
Rasmus

  1. 1 Posted by Raul Riera on 26 Mar, 2011 10:33 PM

    Raul Riera's Avatar

    Could you doble check that line 84 edit? it's the same... just with the 131 it doesn't work :(

    Thanks!

  2. 2 Posted by @bhi on 28 Mar, 2011 03:29 PM

    @bhi's Avatar

    i am using autoSuggest v1.4 and also facing the same problem Raul listed above.

    In the solution stated by Rasmus for Line 84 initial text and changed text are same though Line 84 in original code is :
    var values_input = $('');

    I dont know if Rasmus wants us to change this original code to :
    var values_input = $('');
    But, that wud mean that hidden input field will be removed from DOM.

    AND definitely changing Line 131 as mentioned in the solution did not make any change to "removing all the values from hidden input field" bug. In fact it lead to a new bug:
    The initial text "Enter Name Here" is not cleared automatically when input field gains focus.

    Is there anything we are missing, can anybody suggest ?

  3. 3 Posted by @bhi on 28 Mar, 2011 03:44 PM

    @bhi's Avatar

    i just noticed that the original text that i typed for Line 84 has been changed from
    var values_input = $(' '); To var values_input = $(' ');

    The reason i think is due to this editor neglecting HTML text inside $(). As a result the changes that Rasmus made to HTML of hidden input field on line 84 are not displayed.

    May be if someone knows the fix and can just paste Line 84 like :
    Original Line 84:
    var values_input = $('<
    //------- Begin HTML Input field input type="hidden" class="as-values" name="as_values_'+x+'" id="as-values-'+x+'"
    // ------ End HTML input field />'); It should do the trick around the editor. Again i am just testing this method of typing and don't know how it will post.
    Go to line 84 in jquery.autoSuggest.js and you will see the original text.

  4. 4 Posted by Raul Riera on 28 Mar, 2011 03:47 PM

    Raul Riera's Avatar

    True, now we need to know what changes he meant hehe

  5. 5 Posted by @bhi on 28 Mar, 2011 04:07 PM

    @bhi's Avatar

    Haha...

    i found the solution :
    Add an extra attribute
    value="," to HTML of input on Line 84.

    And a quick fix for problem of start text not disappearing would be to set startText property to "".

  6. 6 Posted by Raul Riera on 28 Mar, 2011 04:12 PM

    Raul Riera's Avatar

    Could you upload this changes to pastie.org or something? or maybe just the whole .js hehe...

    Thanks!

  7. 7 Posted by Robert Muster on 05 Apr, 2011 10:25 AM

    Robert Muster's Avatar

    The problem is really simple.
    Find this code block (line 333 to me):
    var close = $('<a class="as-close">&times;</a>').click(function(){
    and change the line:
    values_input.val(values_input.val().replace(","+data[opts.selectedValuesProp]+",",",") with this one: values_input.val(values_input.val().replace(""+data[opts.selectedValuesProp]+",","")

    Thank you Drew for this great plugin!

  8. 8 Posted by Raul Riera on 05 Apr, 2011 03:54 PM

    Raul Riera's Avatar

    Thank you! that fixes it :)

  9. 9 Posted by dulfe on 10 Jul, 2011 03:05 AM

    dulfe's Avatar

    FYI... you also have to change the line 168 from

    values_input.val(values_input.val().replace(","+last+",",","));

    to

    values_input.val(values_input.val().replace(""+last+",",""));

    so the "backspace" key works correctly.

  10. 10 Posted by Ibrahim Azhar A... on 18 Aug, 2011 04:55 AM

    Ibrahim Azhar Armar's Avatar

    thank you @Robert Muster and @dulfe that fixes my problem too. :)

  11. 11 Posted by Jakie on 06 Sep, 2011 10:53 AM

    Jakie's Avatar

    Thanks to Robert Muster. He solved the problem easily.

  12. 12 Posted by Kiran on 14 Sep, 2011 07:21 AM

    Kiran's Avatar

    @Robert Muster : The problem with this solution is one value is contained in the other value. E.g: you have '123,456,56,' and now you are deleting a selection with value '56', this will result in '123,4,56,' which is incorrect. We needed '123,456,'

  13. 13 Posted by Robert Muster on 14 Sep, 2011 08:29 AM

    Robert Muster's Avatar

    @Kiran I don't understand what is the problem...could you give a link?

  14. 14 Posted by Kiran on 14 Sep, 2011 08:57 AM

    Kiran's Avatar

    @Robert Muster: Please follow the below steps to reproduce the problem:

    1. Define data as follows
      var data = {items: [ {value: "11", name: "elevel"}, {value: "6", name: "six"}, {value: "911", name: "nine-eleven"} ]}

    2. Now type "nine-eleven", "six", "eleven" in the same order in the suggest box. Now the value we have in the hidden field is "911,6,11,"

    3. Now delete the last item, "eleven". Now we expect "911,6," but we will have "96,11," instead. Because "11," matched with "911,"

    Regards,
    Kiran

  15. 15 Posted by Robert Muster on 14 Sep, 2011 12:09 PM

    Robert Muster's Avatar

    I see...
    Try this:

    at line 333 add this (in the function):

    var match_string = new RegExp("\\b"+data[opts.selectedValuesProp]+",",'gi');
    var field_value = values_input.val();
    

    and then change the line
    values_input.val(values_input.val().replace(""+data[opts.selectedValuesProp]+",","")

    with this:

    values_input.val(field_value.replace(match_string,""));

    That should do the trick :)

  16. 16 Posted by Kiran on 15 Sep, 2011 01:00 AM

    Kiran's Avatar

    Thanks @Robert Muster

  17. 17 Posted by DasLight110 on 19 Sep, 2011 08:11 PM

    DasLight110's Avatar

    I found that if I set the prefill to blank the issue of the first item never being removed from the list ot options vanished...
    preFill: [ { value: "", name: "" } ],

    I know this is not a fix but wanted to post it...

  18. 18 Posted by Matt on 24 Oct, 2011 10:06 PM

    Matt's Avatar

    Robert Muster's solution seems to be a good one. But he forgot to also show us the Delete Key portion.

    at line 168 add
    var match_string = new RegExp("\\b"+last+",",'gi');

    and then change the line:
    values_input.val(values_input.val().replace(","+data[opts.selectedValuesProp]+",",","));

    to this:
    values_input.val(values_input.val().replace(match_string,""));

  19. 19 Posted by Robert Muster on 25 Oct, 2011 06:48 AM

    Robert Muster's Avatar

    @Matt I did forgot :) thank you for posting the complete solution

  20. 20 Posted by shogo2040 on 01 Nov, 2011 06:10 PM

    shogo2040's Avatar

    I think I'm going to also change the line 7 comment to:

    • Version 1.4.1 - Updated: Oct. 24, 2011
    • drew.tenderapp.com/discussions/autosuggest/126-major-bug-and-the-solution

    Then I'll have to re-minify it but use the original .minified.js name instead of the industry standard .min.js

    I take it we can't upload both files somewhere like github or here?

  21. 21 Posted by shogo2040 on 01 Nov, 2011 07:15 PM

    shogo2040's Avatar

    After testing, the important part of the problem is fixed (the values to be submitted are cleared when bubble -x- is closed)

    But the other problem of "I could add the first selected item twice" is still there. Anyone else have this issue?

  22. 22 Posted by Jon on 03 Jan, 2012 04:04 PM

    Jon's Avatar

    I don't know if already existed a solution for the "I could add the first selected item twice" problem, but I have found one:

    in function processData (line 265 for me) change:

    line 285 for me:
    if(str.search(query) != -1 && values_input.val().search(","+data[num][opts.selectedValuesProp]+",") == -1 ){

    with:

    if(str.search(query) != -1 && values_input.val().search(","+data[num][opts.selectedValuesProp]+",") == -1 && values_input.val().split(",",1) != data[num][opts.selectedValuesProp]){

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