Hi guys! I need to change the checkbox in my dataset when the first one is clicked, but it only changes on the first item in my repiter and when I click the second field, nothing happens. Can you help me please?
@firmaloizzo I’m not sure I understand your issue correctly, perhaps you can share your code.
In any case, when clicking on an item in a repeater, let it be a checkbox or any other element, you should address the items with $item, rather than the global use of $w which what I think your case it.
For example: You have a repeater with checkbox in it. You want to click a checkbox and check/uncheck only the checkbox you clicked.
$w ( ‘#myCheckbox’ ). onClick (( event ) => changeCheckboxState ( event ));
function changeCheckboxState ( event )) { const $item = $w . at ( event . context ); let itemData = $w ( “#myRepeater” ). data . find (( x ) => x . _id == event . context . itemId );
console.log(“Clicked item data:”, itemData )
console.log(“Checkbox is now:”, $item ( “#invoiceCheckbox” ). checked )
}
@firmaloizzo I see you used the code from the example.
The example code doesn’t fit the repeater case, it is suitable for a standalone editor element. You shouldn’t check the status of an item in a repeater with $w, please use $item.
Consider this code:
$w . onReady ( function () {
$w ( “#checkbox” ). onChange ( event => checkboxHandler ( event ))
});
function checkboxHandler ( event ) { let $item = $w . at ( event . context ); let itemData = $w ( “#afterTmottowrRepeater” ). data . find (( x ) => x . _id == event . context . itemId );
console . log ( “itemData:” , itemData );
**let** isChecked = $item ( "#checkbox" ). checked //Checking the checkbox state of clicked item
**let** isChecked2 = $w ( '#checkbox' ). checked ; //Checking the checkbox state of $w element
console . log ( "isChecked:" , isChecked );
console . log ( "isChecked2:" , isChecked2 );
}
See that " isChecked" prints the status of the checkbox in the item while “isChecked2” always prints false.
@jacobg Thanks for your code! modify it so that when you click on the first checkbox the second change. The problem is that I try to save that and it does not save it.
function checkboxHandler ( event ) { let $item = $w . at ( event . context ); let itemData = $w ( “#repeater1” ). data . find (( x ) => x . _id == event . context . itemId );
console . log ( “itemData:” , itemData );
**let** isChecked = $item ( "#checkbox2" ). checked //Checking the checkbox state of clicked item
**let** isChecked2 = $w ( '#checkbox2' ). checked ; //Checking the checkbox state of $w element