How to insert a TAG with Comma or Semicolon

Asked

Viewed 131 times

3

I found this project to insert TAGS in a form, but it only adds the TAG when enter key, and when it happens in my form it already sends! I would like to know what I should do in the SCRIPT so that it adds the TAG every time I insert comma or comma....

Example form: inserir a descrição da imagem aqui

SCRIPT CODE:

<script>
  $(document).ready(function() {
$('#addTagBtn').click(function() {
                $('#tags option:selected').each(function() {
                    $(this).appendTo($('#selectedTags'));
                });
            });
            $('#removeTagBtn').click(function() {
                $('#selectedTags option:selected').each(function(el) {
                    $(this).appendTo($('#tags'));
                });
            });
 $('.tagRemove').click(function(event) {
                event.preventDefault();
                $(this).parent().remove();
            });
            $('ul.tags').click(function() {
                $('#tags-field').focus();
            });
            $('#tags-field').keypress(function(event) {
                if (event.which == '13') {
                    if ($(this).val() != '') {
                        $('<li class="addedTag">' + $(this).val() + '<span class="tagRemove" onclick="$(this).parent().remove();">x</span><input type="hidden" value="' + $(this).val() + '" name="tags[]"></li>').insertBefore('.tags .tagAdd');
                        $(this).val('');
                    }
                }
            });

  });

</script>

2 answers

4


The line that detects the enter is this:

if (event.which == '13') {

The Code of enter is 13, you can put other conditions:

if (event.which == 13 || event.which == 59 || event.which == 188  ) {

But it is important to note that this code of yours analyzes Keycodes, not characters. This may have some variation depending on the keyboard map used.

Note also that I removed the quotes, I do not understand the reason the original code compare with strings.

To tell you the truth, for what you want, you’d better get a script different, or adapt this to be based on string and not on the pressed key. For example, the enter number no longer works on it.

Here is a tool for online testing:
http://javascriptkeycodes.com/? debug

Test with keypad keys and the main keyboard to better understand the problem. Also test with ABNT2 and international keyboards, and you will notice that the code changes.

  • I can barely see your movements :p

  • @jbueno even though it took me a long time to find the tool kkkkk - However, like yours, it’s right.

  • I just found it strange that for me the code gave 191. Already gives AP a sense that this can be variable

  • Your keyboard is ABNT2?

  • Yes, it’s ABNT2. I was actually seeing this after I read your edition.

  • The browser itself can make a difference.

  • My friend, thank you for your help! I changed what you said and tested the codes on the tool you recommended, here it is 188 and 191... however it didn’t work! I insert the point and comma or comma and nothing happens, but I took the 13 that was enter and it’s not really adding by enter anymore!

  • I did not understand why there are quotes in the original code, I copied it as it was. Then test without. Anyway, I think the script chosen is a bit precarious. It would be good to see alternatives.

  • Bacco, thank you very much! I test now and it worked as follows: if (Event.which == 59 || Event.which == 188) { it separates when I put point and comma, already solve the problem!!! Vlwww!

Show 4 more comments

2

It is in this stretch

$('#tags-field').keypress(function(event) {
    if (event.which == '13' || event.which == '191' || event.which == '188') {
        //...
    }
});

13 is the code of the enter key, switch to 191 which is the semicolon code or 188 which is the comma code.

As (well) noted by Bacco, be careful because this validates the key code pressed and not the character itself, so it is subject to variations.

  • 191 in mine is something else :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.