ichord / At.js

Add Github like mentions autocomplete to your application.
http://ichord.github.com/At.js
MIT License
5.29k stars 671 forks source link

Selecting an element submits form #325

Open ausminternet opened 9 years ago

ausminternet commented 9 years ago

Hi there,

I have a form with a hidden field and an contenteditable div. I bound the enter-key to the contenteditable, to put it's text to the hidden filed and submit the form.

Now on selecting an element from the atwho.js dropdown with the enter-key will insert the atwho.js element AND submit the form. Any ideas how to prevent the enter to submit the form, when it was used to select from atwho list?

pavelivanov commented 9 years ago

I think developers need to add tabindex="" to dropdown list, focus it on visible and focus field back when hide. But now I did this:

var viewJustHid = false;
$element.on('hidden.atwho', function() {
    viewJustHid = true;
    setTimeout(function() {
        viewJustHid = false;
    }, 100);
});

$element.on('keydown', function(event) {
    if (event.which == 13 && !event.shiftKey && !event.ctrlKey && !event.metaKey) {
        event.preventDefault();

        if (!viewJustHid) {
            submit();
        }
    }
});
DDaems commented 9 years ago

This is how i resolved it: http://jsbin.com/gabuwetowu/edit?html,console,output Its nearly the same as grammka his one, only i use the isSelecting event to check if the user is doing a @ mention. I think i prefer grammka his one, and might change my solution so i can drop my keyuplistener.