Adding an HTML element (a button to a form) when an event happens (when another button, from another form, is clicked), using EJS?

StackOverflow https://stackoverflow.com/questions/23414300

質問

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";
...
  1. you do not need innerHTML in a radio button so you can safely remove the following lines:
    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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top