How to fix this undefined error?
Use
$w(‘#input23’).value = random number(10);
geovel, please refer to this api - Wix Editor Elements ($w) - Velo API Reference - Wix.com for additional info and examples
Hi, any advice, I change my code a bit to this after I see Mr. Yoav advice —>
export function button12_click(event) {
//Add your code for this event here:
var x = Math.floor((Math.random() * 1000000000) + 1);
$w(“#idcode”).value = x;
It’s working, I can now generate random numbers, but the number it produce is not displaying on the database, I already connect the textfield (#idcode) to my database but the database do not displaying the value
Hi geovel,
The code you have above is only updating the UI, not the database, and as such the random number generated will not be written to the database.
To write the number to the database, you will need to use one of
- using forms to write the data to the database - creating a custom form
- using the wix data api directly.
Thank you for the response Yoav! Do you know some script that will update my database so that the numbers will insert into it? Please really need this
Hay Geoval,
The very basic thing to do is to use wix code insert. Without more understanding of your data model and what you try to do, I can only give you a very basic code snippet that you will need to adjust to your needs.
It can look something like the following -
import wixData from 'wix-data';
export function button12_click(event) {
//Add your code for this event here:
var x = Math.floor((Math.random() * 1000000000) + 1);
$w("#idcode").value = x;
wixData.insert('COLLECTION_NAME', {number: x});
}
Thank you Yoav.
Hi Yoav,
Could you please look at my code, cause it’s acting weird or I just do it incorrectly.
$w.onReady( function() {
function randomNumber(len) {
var randomNumber;
var n = '';
for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
}
$w('#input25').value = randomNumber(10);
} );
export function button10_click(event, $w) {
//Add your code for this event here:
var x = $w('#input25').value;
wixData.insert('regdog', {code: x});
}
I manage to create a random number after my create page load
but when I click submit it, it didn’t work the way it should be, it’s just create 3 columns on my data collection, please see the image below.
any advice or code to make it correct, that the ID code (code) will go to it’s right path?
Hi,
In your page #button10 has two roles, one for submit and the other onClick event. that is not a good practice to make a button do more then one event. instead of using wixData.insert just connect the input element to the dataset like you did with the rest of the input fields.
Good luck!
Roi
Hi Roi, I already did that but it didn’t work
$w.onReady( function() {
function randomNumber(len) {
var randomNumber;
var n = '';
for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
}
$w('#input25').value = randomNumber(10);
} );
the numbers will not display on the input element if I connect it to the data base
Hi, I want to click on picture then go to products page and there to be shown an specific products category.
Here is my code:
$w.onReady( function () {
$w(“#image14”).onClick((event) => {
console.log(“test…find… …”)
wixLocation.to(“/jewelry-collection”);
wixData.query(“HANDMADE”)
.find()
.then( (results) => {
if (results.items.length > 0) {
let firstItem = results.items[0];
} else {
console.log(“not fond filter category …”)
}
} )
. catch ( (err) => {
let errorMsg = err;
} );
});
what is wrong?
Also I tried the following code:
$w.onReady( function () {
$w(“#image14”).onClick((event) => {
console.log(“test…find… …”)
wixLocation.to(“/jewelry-collection”);
wixData.query(“HANDMADE”)
.find()
});
});