Javascript – Dynamic dropdown list in the jsp

javajavascriptjspjstlservlets

I have a dropdown list which is dynamic so how can I iterate and insert the list values in the dropdown list?

var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};

<select name="questionid" id="questionids">
<c:forEach var="questionids" items="${questionids}">
<option value="">${questionids}</option>
</c:forEach>
</select>

I tried as mentioned above. it is not working. Also I need the value of each option should auto increment like, For first option value is 1 and second option value is 2. e.t.c

Can any one please suggest the solution please

Best Answer

This is right approach but you have to give another name to var in <c:forEach> like below:

var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};

<select name="questionids" id="questionids">
    <c:forEach var="questionid" items="${questionids}">
        <option value="${questionid}">${questionid}</option>
    </c:forEach>
</select>