Saw a code like this:
index.html
<script type = "text/javascript" src = "choices.js" ></script>
<form id = "myForm" action = "">
<p>
<label> <input type = "radio" name = "myChoice" value = "A" />
A </label>
<br />
<label> <input type = "radio" name = "myChoice" value = "B" />
B </label>
<br />
<label> <input type = "radio" name = "myChoice" value = "C"/>
C </label></form>
<script type = "text/javascript" src = "choicesDom.js" ></script>
choices.js
function userChoice (choice) {
var dom = document.getElementById("myForm");
for (var index = 0; index < dom.myChoices.length; index++) {
if (dom.myChoices[index].checked) {
choice = dom.myChoices[index].value;
break;
}
}
choicesDom.js
var dom = document.getElementById("myForm");
dom.elements[0].onclick = userChoice;
dom.elements[1].onclick = userChoice;
dom.elements[2].onclick = userChoice;`
Question is: Why is it that in choicesDom.js, userChoice is being called like a variable? Why is a parameter not required, and Why is it not userChoice() or userChoice(value)? When tried, it was shown as a syntax error.
What is the rule of a function for javascript? It seems to be quite loosely used as compared to other programming languages' function