Hi everyone!
I'm struggling for days now trying to count the number of spaces (aka words) in a user input. It would works with a Key Press Event but it doesn't work when you copy and paste a piece of text...
Does someone have an idea? A lead?
Many thanks!
Hi Paul,
I can see why you've been working at this for a while. After some trial and error, I stumbled upon a solution. It turns out that trapping for the "v" key and either the control or command key (Mac) being pressed, and then calling the blur event of the input (with the WordCount function in it) gives you the word count. Immediately after the paste and before calling the blur event, the value of the input does not change to reflect the pasted text. The blur call does update the value.
export function textBox1_keyPress(event) { let key = event.key; let cmdkey = event.metaKey; // command key on a Mac let ctrlkey = event.ctrlKey; // control key let nWords = WordCount($w('#textBox1').value); console.log("onKeyPress word count: " + nWords); if (key === "v" && (ctrlkey === true || cmdkey === true)){ $w('#textBox1').blur(); nWords = WordCount($w('#textBox1').value); } } export function textBox1_blur(event) { let nWords = WordCount($w('#textBox1').value); console.log("Word Count from blur: " + nWords); } function WordCount(str) { return str.split(' ').filter(function(n) { return n !== '' }).length; }