Pixabay/jQuery-tagEditor

Allow enter keypress with no tag changes to fire against underlying input

Opened this issue · 0 comments

A "double-keypress" i.e. pressing enter after doing tag work, then enter again, or just hitting enter without doing any tag work, should behave as pressing the enter key in an HTML input. It should fire the keypress event with code 13.

This is useful because the form without this plugin might have wanted to take action on changes that are intentionally different than tag editing. I.E. form submission.

I modified it to work, but don't seem to be permitted to push a branch. So....starting with line 311 of v1.0.21...here's my changes to get this to work the way I think it should.

// enter key
                else if (e.which == 13) {
                    var next_tag = $t.closest('li').next('li').find('.tag-editor-tag');

                    if (next_tag.length) 
                        ed.trigger('click', [closeTags]);
                    else if ($t.val()) 
                        ed.click();
                    else 
                    {
                        // A "double-enter" press.  they hit enter but nothing was edited.  
                        // The consumer may want to bind this to an enter keypress on the original input, and perform an action.
                        // So fire that event.
                        var ee = $.Event('keypress');
                        ee.which = 13;

                        el.trigger(ee);
                        return false;
                    }
                        

                    // trigger blur if maxTags limit is reached
                    if (o.maxTags && ed.data('tags').length >= o.maxTags) {
                        ed.find('input').blur();
                    }
                    return false;