When I submit how to get all checkbox state and notes using useState in hooks
function handleEditformsubmit(e: any) {
e.preventDefault();
var errids = [];
var arr_length = InspectionList.length;
var chkbx = "chkbx_";
var notes = "notes_";
var data = [];
for (var i = 1; i <= arr_length; i++) {
let chkbx_state: any = (document.getElementById(chkbx + i) as HTMLInputElement).checked;
let user_note: any = (
document.getElementById(notes + i) as HTMLInputElement
)?.value;
if (!chkbx_state && !user_note){
document.getElementById(notes + i)
console.log(notes+i)
errids.push(notes+i)
setErrId(errids)
}
let row_data = Object();
row_data["id"] = i;
row_data["is_checked"] = chkbx_state;
row_data["notes"] = user_note;
data.push(row_data);
}
setInsData(data)
errids = []
}
This is working fine, but I would like to learn How to solve using states. Any advise please thanks
2
Answers
In this code:
We maintain the state of your inspection list using useState.
We use handleCheckboxChange and handleNoteChange functions to update the state when a checkbox or note input changes.
In handleEditFormSubmit, we iterate over the list to check for errors, and we set errIds accordingly.
Finally, we use the state variables to display the checkboxes, notes, and error messages in the component’s JSX.