The original code will likely work if you put it in the dataset’s onReady()
So
$w.onReady(() => {
$w('#dataset1').onReady(() => {
$w('#button2').onClick((event, $w) => {
});
});
}
The reason being that the button is part of a repeater that depends upon the dataset items. The if you don’t wait for the dataset to load before setting up the handlers they may not work as expected.
Hope that helps.