Being a novice in JavaScript, I decided to challenge myself by attempting to restore a form using cookies when the page is closed or reloaded. Here are the steps I've taken:
- Created an array with form selections
- Converted it to a string
- Set up a cookie
- Retrieved the cookie
- Extracted the string
- Converted it back to an array
So far, everything was going smoothly. However, I encountered an issue at the next step...
- Trying to pass the array values to restore the form elements
Despite having the correct form selections present in the retrieved array, whenever I reload the page, only the last radio button selected (even if I change the number of buttons) and all checkboxes are checked.
I have attempted various solutions to identify the error, even converting the "true"/"false" values to Booleans, but nothing seems to work.
This is how my code looks currently:
frm[0]=true
frm[1]=false // for two radio buttons
frm[2]=false
frm[3]=false // for two checkboxes
document.getElementById("myradio1").checked=frm[0]
document.getElementById("myradio2").checked=frm[1]
document.getElementById("mycheckbox1").checked=frm[2]
document.getElementById("mycheckbox2").checked=frm[3] // passing the array values to the form elements
Unfortunately, no matter what values I pass, only the last radio buttons and all checkboxes get selected.
While I understand that this code may not be following best practices in JavaScript, I would like to first comprehend why it is behaving like this before making any changes. Any insights or comments you can provide would be greatly appreciated as I am eager to resolve this issue.