Tural Ali
Tural Ali

Reputation: 23240

Javascript word counter issue

What I want to do is, to make counter of total entered words which will save results even after cleaning text area.

Please take a look at the result of code below: http://jsfiddle.net/mnvMn/5/

NOTE: If you want to fix this problem by changing setInterval(autoPost, 100); DON'T! Because I tested all available change(), paste(), keyUp().... functions. No one of them worked with mobile barcode scanner. Only setting interval works.

var form=$("#bcscanner"), ids=$('#itemids'), counter=0;
function count() {

    var value =$(ids).val().replace(" ", "").replace(/[\s,]+$/, '');
    var words = value.split(",");
    var numWords =words.length;
    if($(ids).val() === '') {
        numWords = 0;
    }
    return numWords;
}

function autoPost(){
    counter=count()+counter;
    $("#counter").html(count()+'/'+counter);
    if(count()==="10"){   
       ajaxpost();     
       $(ids).val('');
    }
}

$(document).ready(function () {   
    $( "input:submit, input:reset").button();

    setInterval(autoPost, 100);

}); 

Upvotes: 0

Views: 338

Answers (2)

Phil McCullick
Phil McCullick

Reputation: 7170

Here is a modified version of your fiddle that counts the total words correctly: http://jsfiddle.net/MuyaK/5/

You were updating the global counter on each update instead of tracking the total number of ten word counts.

Upvotes: 1

Muthu Kumaran
Muthu Kumaran

Reputation: 17910

You can use jQuery plugin to count words, you can restrict typing either by character or words http://qwertypants.me/counter/

Upvotes: 0

Related Questions