I added the word count to my page.
export function input1_keyPress(event, $w) {setTimeout(() => { let val = event.target.value;if(val.length === 0)return;let count = WordCount(val);$w("#count").text = "" + count;}, 10);}function WordCount(str) {return str.split(" ").length;}
I have 2 issues:
1- It adds the space after the last word in the count.
2- I want to add the count to a dataset field but for some reason it doesn’t want to write. Is there something special to do ?
Thanks
Well first of all, you should change your code-structure, which is totaly terrible to read.
It would be better if it would look like this one…
export function input1_keyPress(event, $w) {
setTimeout(() => {
let val = event.target.value;
if(val.length === 0){return}
let count = WordCount(val);
$w("#count").text = "" + count;
},10);
}
function WordCount(str) {return str.split(" ").length;}
- Where is the code-part which will write/save data into your DB ?
There is absolutely no CODE-LINE for that action.
If you have connected your dataset this will not really work like you want. You will have also to code it.
Mixing CODE with connected dataset in the property-panel is not a good idea and causes in most cases failures and inteferences.
Thanks Velo-Ninja, would you have a line of code for me to try? The name of my database is “scenes” and the field is “word count”
@ericheuser
How to write to DB ?
You can do it by using the dataset, or you do it the WIX-DATA-WAY.
I will show you the second one → by using INSERT…
You will need something like this one…
import wixData from 'wix-data';
const DATABASE = "" // <-- put in here your Collection-ID.
$w.onReady(()=>{myFunction();});
function myFunction(){
let toInsert = {
"title": "Mr.",
"wordcount": 1000, //<--- your Word-Count goes here...
};
wixData.insert(DATABASE, toInsert)
.then((results)=>{let item = results; console.log(results)})
.catch((err)=>{let errorMsg = err;});
}
Good luck.
I have multiple fields on my page that wright to a data set with a submit button. I also have code to create a word count in a textbox call count1.
I would like the number in that field to be written in the database when I click on the submit button.
I think I would need to change the onReady by something else.