How can I index into a dataset?

Hi Corvid Community!
How can I index into a dataset? On a button click, I would like to display one word from that dataset.
To better put the question into context, this is for a game where multiple users submit words into a dataset. On the click of this button, a random word would be chosen from that dataset (named ‘wordslist’) and displayed on the website.
I tried to program the onClick function but I get the error “save operation failed: DatsetError: Some of the elements validation failed”. I have also connected the button and the text box to the dataset as well as the button’s action to ‘submit’.
Here is what I have tried:


export function pickaword(event) {
 //Add your code for this event here: 
    console.log(wordslist)  
 var randomNumber = Math.floor(Math.random()* wordslist.length));
    document.getElementByID("#randworddisplay").value = wordslist[randomNumber]
}

Thank you in advance for any suggestions and advice!

Hi,
If I understand correctly, you want to get the random number and then display an item with the index equal to that number, an example of code is below:

let count = $w("#myDataset").getTotalCount();
let randomNumber = Math.floor(Math.random()* count));
$w("#myDataset").setCurrentItemIndex(randomNumber);

You can find references to the code above here and here.

Got it, thanks so much!

I’m still having a bit of trouble displaying that word in the text box. I’ve tried this:


 var x = $w("#wordslist").setCurrentItemIndex(randomNumber);
    $w('#randworddisplay').text = x


and have also tried
document.getElementById(“#randworddisplay”).value = x
and tested both by setting it equal to “hello”. Neither seem to work. Perhaps I have an error in the code or maybe some of my settings are incorrect?

The “pick a word” button is connected to the dataset and the action is “submit”. I have also tried the text both connected and disconnected to the dataset.