1

I have this code and I just want to remove comma to the last value that the user will insert. If I am going to click the add button the comma to the last will be remove. Code:

$(function() {
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }

    $( "#tags" )
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).data( "ui-autocomplete" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        minLength: 0,
        source: function( request, response ) {
          // delegate back to autocomplete, but extract the last term
          response( $.ui.autocomplete.filter(
            availableTags, extractLast( request.term ) ) );
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          // remove the current input
          terms.pop();
          // add the selected item
          terms.push( ui.item.value );
          // add placeholder to get the comma-and-space at the end
          terms.push( "" );
          this.value = terms.join( ", " );
          return false;
        }
      });
  });
user00602
  • 37
  • 1
  • 7

1 Answers1

0

my understanding is that if you had items apple, mango, banana, (blank array item) You would be left with this string "apple,mango,banana," to remove the last comma you would to a simple substring to remove last character

val = val.substring(0,val.length -1)
Cyassin
  • 1,437
  • 1
  • 15
  • 31
  • What I mean is that, if I have values like: 'Apple, Mango, Banana, ' how to remove the last comma like this 'Apple, Mango, Banana'? – user00602 Mar 13 '14 at 06:13
  • That is what my answer supplied does. It removed the last character from a string, which in this case is the "," – Cyassin Mar 13 '14 at 06:16
  • Where should I insert your given code? – user00602 Mar 13 '14 at 07:19
  • 2
    Your line this.value = terms.join( ", " ); Place after it: this.value = this.value.substring(0,this.value.length - 1); – Cyassin Mar 14 '14 at 00:16