user1943674 user1943674 - 2 months ago 26
Node.js Question

Express.js Radio buttons as a form post method using render params

I am using Express.js and EJS to render the views. I am building a small web app and passing in variables to the render of a certain page. The page will use a for loop to generate a set of radio buttons based on the size of the array I am passing it. The radio buttons have values and I will let the user select an option and then pass it back as a post method. Here is the code on the rendered page. The issue is that, the radio buttons are being generated independently (allowing me to select all of them...not one or the other in typical radio button fashion).

<div class="answers">
<h2 class="movie-header">Select One:</h2>
<form method="post">
<% for (var i = 0; i < answers.length; i++) { %>
<input type="radio" name="answer<%= i %>" value="<%= answers[i] %>"> <%= answers[i] %><br>
<% } %>
<input type="submit" value="Submit">


You should use the same name attribute on each radio button to create a "radio button group" which will only let you select one value out of a group of buttons. See the type "radio" on this page: MDN html input

<input type="radio" name="answer" value="<%= answers[i] %>"> <%= answers[i] %><br>