javascript - count number of multiple selected in jquery autocomplete field -


in jquery's autocomplete multiple value, try add counter selected values. should updated immediately, when chose/add 1 more values.

both codes of workaround not working.

1

function checkreceiver() { var str = document.forms[0].tags.value, regex = /417/igm, count = str.match(regex), count = (count) ? count.length : 0; console.log(count); document.forms[0].receiver.value=count; } 

2

var str = document.forms[0].tags.value; count = (str.match(/417/g) || []).length; document.forms[0].receiver.value=count; 

is there trigger, can use in autocomplete function directly?

demo on jsfiddle

you quite close working solution. propose following changes:

instead of testing regex, split on commas: plugin using puts comma automatically after each new selected value. therefore, can count number of values number of commas. example:

function checkreceiver() {     var str = document.forms[0].tags.value;      var words = str.split(',');                   // split on commas     var count = words.length - 1;                 // correction first element     document.forms[0].receiver.value = count; } 

alternatively, split on spaces:var words = str.split(' ');, if think users might delete last comma. splitting on spaces assumes @ least space between 2 values.

make sure function called on change: make sure checkreceiver() function called @ possible change, need make sure captures events. 1 way of capturing events in <input> element forcing check upon possible change. example (method comments in answer):

    <input type="text" name="numbers" id="tags" class="input_text clearable" size="50"     onchange="checkreceiver();"      onkeypress="this.onchange();"      onpaste="this.onchange();"      oninput="this.onchange();"      placeholder="choose receiver" tabindex="1" value="" autofocus> 

additionally, want call checkreceiver() when autocomplete list closes. therefore:

    .autocomplete({        close: function(event, ui) {           checkreceiver();                        // call checkreceiver() upon close        }, ... 

with these changes in place, program works expected. see this jsfiddle.

i can recommend take @ jquery tokeninput designed multiple-input autocomplete text entries, , works in intuitive manner. example, tokeninput can use onadd , ondelete callbacks (see here). these callbacks, can increase (onadd) , decrease (ondelete) counter variable.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -