Can't select a dataset successfully

I’ve got a site with a dynamic page (a list of gigs) that I need to add buttons for ticket purchases to. I’ve set up the URL field and linked the buttons, which all works OK, but I need to hide the buttons where there isn’t a valid URL. The dataset is referenced as #dataset1 in the page, and the code is as follows:

$w.onReady( function () {
let activeDynamicItem = $w(“#dataset1”).getCurrentItem();
console.log(activeDynamicItem);
let fieldX = activeDynamicItem[‘tickets_url’];
console.log(fieldX);
// if (fieldX != ‘#’) {
// $w(‘#button1’).show();
// } else {
// $w(‘#button1’).hide();
// }

});

I’ve commented out the hide function at the moment as the issue I’m having is that the consolelog for activeDynamicItem produces ‘NULL’, and then the script stops. I’m just getting a single ‘null’ item back, and that’s it.

Can anyone point me to what I’m doing wrong? I know I’ll need to iterate through all the items (there are multiple listings in the page), but I’m getting nowhere, despite reading the docs on getCurrentItem.

Obviously if there’s a better way to do this (i.e. filter the creation of the button from the editor) then that would be handy, but I couldn’t find it.

Greetings,

See the second example of getCurrentItem . You need to make sure that the dataset is loaded before calling any of its functions.

Your dataset is probably not yet available to use. See the forum post Datasets have an onReady function .