2

I am using jQuery autocomplete with multiple input boxes interconnected, i.e. when inserting some value in the first input box and losing focus, the following input boxes get affected (i.e. values are filtered accordingly). Then I can focus and another input box and start typing some value, so that I get both filters applied (typed value and according to previous input box value).

Also, I setup an event for cleaning ALL input boxes when deleting all characters from an input box:

if (inputBoxTrigger.val().length == 0 && (event.keyCode == BACKSPACE || event.keyCode == DELETE)) {
        jQuery.each(inputObjectArray, function (i, obj) {
            obj.val("");
        });
    }

So, I cleanup everything when I just removed last character in the current input box. However, I could do the following: 1) Select the content of an input box (e.g. via mouse or holding shift) 2) Start typing something in this input box without pressing backspace or delete buttons

In this case, the input box gets cleared and a new character is inserted, but I do not catch this situation. I attempted to drop the check on the backspace/delete button keyCode, but is does not work: when leaving an input box, the new input box is empty, so I just clean up everything (including previous input boxes).

Any ideas?

Manu
  • 4,019
  • 8
  • 50
  • 94
  • can't you just detect a change in the character count in the input box being used, and if it drops, you have your case of clearing them all? – Matthias Nov 22 '16 at 11:53
  • You mean that when the text length drops to 1, I could clear everything? That's not viable: I would have mispelled something, e.g. hhome instead of home, and just going back to remove my second h; when passing from "hh" to "h", I would trigger the event, which is not what I want. I just need to to that when replacing the entire input text with a new character. – Manu Nov 22 '16 at 11:57
  • I don't see why the boxes should have different behavior when you go from "hhome" to "h" vs going from e.g. "whatsup" to "h" (by selecting and typing h). But if that's what you really want, I suspect you might have to detect if characters are selected, and then detect if all characters are selected and if the character count then becomes 1. Could also detect if the character count is 1 and check whether there was a backspace or not (if backspace you have the "hhome" case, if not, you have the "whatsup" case) – Matthias Nov 22 '16 at 12:05
  • this will fail though (I assume) if someone selects everything and pastes something – Matthias Nov 22 '16 at 12:05

1 Answers1

0

Maybe a more complex algorithm to detect string similarity?

If strings are very similar then it may be a simple correction, if not then it may be a completely different item and you may reset the form. You can also chain a length/direction comparison with a similarity comparison to fine tune it (i.e. do the similarity comparison only when going from a long string to another long string, ...).

On string similarity:

Compare Strings Javascript Return %of Likely

javascript text similarity Algorithm

http://andrew.hedges.name/experiments/levenshtein/

Community
  • 1
  • 1
Marc Compte
  • 4,579
  • 2
  • 16
  • 22
  • That may seem a good solution, but string similarity with 2 or 1 characters seems overcomplicated to me. I would expect some sort of event to intercept... – Manu Nov 23 '16 at 09:33