I have multiple fields on my page that wright to a dataset with a submit button. I also have code to create a word count in a textbox call count1.
I would like the number in the wordcount textbox (count1) 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.
If the total field in the collection is defined as Number , you will need to convert the #count text field to a Number in order to save it to the database collection. Do something like this:
let wordcount = Number($w('#count').text);
let toInsert = {
"total" : wordcount
};
Also, please observe the community guidelines and refrain from multiple posts on the same topic or question. Thank you.
Hi Yisrael,
Thank you for your answer.
The code is giving me 2 errors:
1- It creates an empty line (See image line 2)
2- the total field is still empty.
import wixData from ‘wix-data’ ;
export function richTextBox1_keyPress_1 ( event , $w ) {
setTimeout (() => {
let val = event . target . value ;
if ( val . length === 0 )
return ;
let count = WordCount ( val );
$w ( “#count1” ). text = “” + count ;
}, 10 );
}
function WordCount ( str ) {
return str . split ( " " ). length ;
}
const DATABASE = “Scene” // ← put in here your Collection-ID.
$w . onReady (()=>{ myFunction ();});
function myFunction () {
let wordcount = Number ( $w ( ‘#count’ ). text );
let toInsert = {
“total” : wordcount
};
wixData . insert ( DATABASE , toInsert )
. then (( results ) => { let item = results ;
console . log ( results ) })
. catch (( err ) => { let errorMsg = err ; });
}
You should realize that in order to create a line in the database collection, it’s not enough to just include the total field - you will need to include all of the fields that you want to insert in the toInsert object.
Perhaps $w ( ’ #count ’ ) has no value which is why that field is empty as well? Try a console.log() statement to see what the value is.
If you are still having problems, please post the URL and explain where and how to see the problem.
I don’t understand why I need this part of the code that isn’t linked to the submit button. It sends the info when I load the page and no data has been entered yet… Why doesn’t the submit button simply send the data from this textbox like all the other textboxes on the page. It’s like since the wordcount was entered automatically from a script and not entered from a keyboard it doesn’t see it…
Aha - so you have a dataset. I didn’t understand what you were trying to do since you were trying to insert wordcount using wix-data.insert() .
In order to save wordcount to the total field in the collection via the Submit button, you need to set the dataset field value using setFieldValue() . You can do something like this:
$w("#dataset1").setFieldValue("total", Number($w('#count').text));
Then when the Submit button is clicked, all of your fields, including total, will be saved to the database collection.
Note: It is recommended not to mix wix-data code together with wix-dataset code.
Thanks Yisrael, You’re a genius! It worked! I had been struggling with this for a while now.
@ericheuser If I was a genius, I would’ve figured it out earlier.
Where would be the best place to learn this coding… It looks like JavaScript but not exactly…
See Coding with Velo for a quick introduction to Velo Javascript.
To learn about programming with Velo, read the following articles that will help you start working with Velo: