A very useful advice you can give me is what set of tools you recommend me to use (for JavaScript programming).
Currently I am at a decent basic level of JavaScript knowledge. Still I cannot avoid several syntax mistakes from time to time. And I am doing debugging by toggle commenting over code lines. I have Sublime Text as text editor and nothing else. I suppose a linter and a debugging tool will be a must. Or also a more useful text editor? But I cannot decide what to choose. Seems all have pluses and minuses…
I appreciate, as usual, if you share your experience about these support tools.
I try a different approach for storing radio button selection with cookies.
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
<body onload = "checkCookie()">
<form name="myform" id="myform">
<p><input type="radio" name="myradio" id="myradio1" onchange = "checkForm(); setCookie('formString',frmString,1)">
<input type="radio" name="myradio" id="myradio2" onchange = "checkForm(); setCookie('formString',frmString,1)"></p>
</form>
<script>
function checkForm()
{
var frm = new Array()
frm[0] = document.getElementById("myradio1").checked
frm[1] = document.getElementById("myradio2").checked
frmString = frm.join()
alert(frmString)
}
function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
function checkCookie()
{
var frmString1 = getCookie("formString");
var frm1 = frmString1.split(",")
alert(frm1[0])
alert(frm1[1])
document.getElementById("myradio1").checked = frm1[0]
document.getElementById("myradio2").checked = frm1[1]
}
</script>
</body>
</html>
I used some alerts to check if everything is fine and it is. Until the last step when I try to pass the value of frm1[i] to radio buttons.Seems they don’t care about frm1[i] value Any would be frm1[i] value, the second radio button stays checked permanently when page reloads.
Obviously I am doing a mistake, but after hours of checking I was not able to realize what is wrong.
Probably how I try to pass the frm1[i] value to buttons is faulty, but I don’t realize why…
Thank you for the solution including a practical example of using EventListeners.
If I want to use this solution for checkboxes (instead radio buttons) what do I have to change?
About ignoring “the question”: I had no intention to do this. About what question do you speak?
You asked me to provide the console.log output and I did this. I did not notice any other question, sorry for this.
And I still want to understand which was the mistake in my method (except using EventHandlers, which I understood). Why with all correct elements the buttons do not select correctly?
I actually wrote a blog post on how to do this recently.
The blog post uses jQuery, but it should be easy enough to convert to plain JS.
Have a look at that and let me know if you have any questions.
Oops. I completely misread your response. Sorry, that was my mistake.
I struck through my comment in my post above to reflect the fact.
Were you attempting this locally or on a server?
The reason I ask is that some browsers (such as Chrome) don’t allow you to access cookies locally.