input to keep adding number to one field in database

i want an input box to add a number to the current number in one field in the database instead of just listing the numbers. Like if i input a number, it gets added to the value of the first cell in the data base, The rest of the fields stay blank. I know basic javascript but not sure how to do this. Maybe a custom hook using onInsert or adding onClick script to the submit on input page? Thanks

Hey,

It is possible to achieve, using wix-data, with code more or less like this:

import wixData from 'wix-data';

$w("#mySubmitButton").onClick(function() {
    wixData.query('myCollectionName').find().then(results => {
        const record = results.items[0]; // Take the first record
        record.myNumericField += Number($w('#myNumericInput').value);
        wixData.save('myCollectionName', record);
    });
});

Good Luck,
Itai

2 Likes

Itai’s answer is pretty much what you want. I just wanted to point that fields in a database collection are accessed by the fieldId and not by position. You don’t add “to the value of the first cell in the data base”. Rather, you set the value of the field with the fieldId (as in Itai’s example) myNumericField .

2 Likes

The code looks great. But when i insert this into mySubmitButton I get “error: The element selector function (usually $w) cannot be used before the page is ready”

My code looks like this

Right, simply wrap your code like that:

import ...

$w.onReady(function() {
    // put the code here
})
2 Likes

Hello, I am tryin to add 300 to a field credit in my credit database but my codes seems not to be working.

import wixData from ‘wix-data’;

$w.onReady( function () {$w(“#button1133”).onClick( function () {
wixData.query(‘credit’).find().then(results => {
const record = results.items[0]; // Take the first record
record.credit +=Number(300);
wixData.save(‘credit’, record);
});
});

Hello, I am tryin to add 300 to a field credit in my credit database but my codes seems not to be working.
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(" #button1133 ").onClick( function () {
wixData.query(‘credit’).find().then(results => {
const record = results.items[0];
record.credit +=Number(300);
wixData.save(‘credit’, record);
});
});