I am building a Node.js server and using Express. In an EJS view, I have a form with an event listener, that is, when a given radiobutton is clicked, a function is called:
// the way thearray is created is omitted
<% for(var i=0; i<therray.length; i++) { %>
// parts omitted
<td>
<form id= <%= "question_examen"+i %> > // the form that triggers the event
<input type="radio" id= <%= "examen_non"+i %> name="examen" value="non" checked>Non
<input type="radio" id= <%= "examen_oui"+i %> name="examen" value="<%= i %>" onClick= <%= "exam("+i+")" %>>Oui
</form>
</td>
<td>
<form id= <%= "question_selection"+i %> > // the second form, empty at the beginning, but that I want to enrich (see below)
</form>
</td>
// parts omitted
<% } %>
I want the function exam(i), that is trigerred by the first form, to add new radiobuttons to the second form. This is my function:
<script type='application/javascript'>
function exam(i) {
alert(i); // just to check it is correctly triggered (it is the case)
// code with mistakes, I guess, since it's not working:
document.createElement("inputnon");
input.type ="radio";
input.id= <%= "selection_non"+i %>;
input.name="selection";
input.value="non";
input.innerHTML="Non";
document.getElementById("#question_selection"+i).appendChild(inputnon);
document.createElement("inputoui");
input.type ="radio";
input.id= <%= "selection_oui"+i %>;
input.name="selection";
input.value="oui";
input.innerHTML="Oui";
document.getElementById("#question_selection"+i).appendChild(inputoui);
}
</script>
Many mistakes indeed :)
1) you are trying to access the second form using the wrong id
<form id= <%= "selection_examen"+i %> >...
...
document.getElementById("#question_selection"+i).appendChild(inputoui);
2) you need to concat the input's id using straight js, no EJS is needed.
To fix it do this:
input.id= "selection_non"+i;
Instead of:
input.id= <%= "selection_non"+i %>;
3) you are creating an element without declaring and assigning it to a variable, and the element you were creating wasn't a valid input
To fix it do this:
var inputnon = document.createElement("input");
inputnon.type ="radio";
...
Instead of this:
document.createElement("inputnon");
input.type ="radio";
...
input.innerHTML="Non"; ... input.innerHTML="Oui";
Try the following code (did not run it though, so it might have more errors):
<script>
function exam(i) {
alert(i); // just to check it is correctly triggered (it is the case)
// hopefully the following code will run:
var inputnon = document.createElement("input");
inputnon.type ="radio";
inputnon.id= "selection_non"+i;
inputnon.name="selection";
inputnon.value="non";
document.getElementById("question_selection"+i).appendChild(inputnon);
var inputoui = document.createElement("input");
inputoui.type ="radio";
inputoui.id= "selection_oui"+i;
inputoui.name="selection";
inputoui.value="oui";
document.getElementById("question_selection"+i).appendChild(inputoui);
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.