on changue checkbox changue other checkbox

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?

 $w.onReady(function () {
  $w("#checkbox1").onChange((event) => {

      let isChecked = $w('#checkbox1').checked;
    if(isChecked===true){
 
     $w('#dataset1').setFieldValue("nopagar",true);
    $w("#dataset1").save();
}

});

});

What do you mean by “the checkbox in my dataset” checkbox is a UI element. There’s no checkbox in your dataset.

you are right sorry i meant boolean in my dataset

@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 )
}

This code is nice. I don’t know if it helped to Christian, but I will use this code in my projects. Thanks.

1 Like

Hi Jacob! thanks for your time. to make myself understand better I prepared this image .

@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.


$w . onReady ( function () {
$w ( “#checkbox1” ). onChange ( event => checkboxHandler ( event ))
});

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 

if ( isChecked === true ){
$w ( ‘#checkbox2’ ). checked = true ;
$w ( “#dataset1” ). save ();
}
console . log ( “isChecked:” , isChecked );
console . log ( “isChecked2:” , isChecked2 );

}