javahtmljspcomboboxscriptlet

how to repeat the code a number of times depending on the value of a combobox ? JSP Java


I have a view that has a combobox with values from 2 to 25 made with a <c: ForEach> (as seen in the code). I want the form to be repeated, that same amount of times (the value of the combobox). But I have already tried many things and I have searched many pages, but there is nothing specific for this case, that is, I cannot put js code inside the scriplet to obtain the number, I leave the code here:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">

    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-sm-12">
                    <h1>Aca vas a crear tus preguntas</h1>
                    <br>
                    <br><br><br>
                    Cantidad de preguntas:
                    <select id="cantPreguntas"  onchange="getSelectedValue();">
                        <c:forEach var="p" begin="2" end="25">
                            <c:choose>
                                <c:when test="${p==2}">
                                    <option value="${p}" selected="selected"><c:out value="${p}"></c:out></option>
                                </c:when>
                                <c:otherwise>
                                    <option value="${p}"><c:out value="${p}"></c:out></option>
                                </c:otherwise>
                            </c:choose>

                        </c:forEach>
                    </select>
                    <br><br><br><br>

                    <p id='cont'></p>

                    <c:forEach begin="1" end="3">
                        <form class="form form-goup">
                            <label for="txtPregunta">Pregunta num: (num) </label>
                            <input type="text" name="txtPregunta" id="txtPregunta">
                            <br>
                            Cantidad de respuestas
                            <select>
                                <c:forEach var="r" begin="2" end="8">
                                    <option value="${r}"><c:out value="${r}"></c:out></option>
                                </c:forEach>
                            </select> <br>
                            Respuesta (num)<input type="text" name="txtRespuesta" id="txtRespuesta"> <input type="checkbox">
                            <br><br><br><br>
                        </form>
                    </c:forEach>
                </div>
            </div>
        </div>
        <script>
            function getSelectedValue() {
                var selectedValue = document.getElementById("cantPreguntas").value;
                document.getElementById("cont").innerHTML = selectedValue;
            }
            getSelectedValue();
        </script>
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>



    </body>
</html>

I made a function with js that obtains the value of the combobox, and to demonstrate that it works I have created a < p id='cont' > tag to show the value of the combobox

Currently to see some result in the view, I have set it to repeat 3 times (foreach begin = '1' end = '3'), I tried to put many things inside the "end" but everything gives me an error or not works

In summary I want to show a form the number of times the combobox says

currently looks like this


Solution

  • You cannot just generate forms depending on your select-box you need to either submit that select-box and then pass the selected value to your <c:forEach.. .Alternate way using js you can just use clone to clone first form then just passed this cloned html to your div where you need to populate forms.

    Demo Code:

    function getSelectedValue() {
      var selectedValue = document.getElementById("cantPreguntas").value;
      document.getElementById("cont").innerHTML = selectedValue;
      var cloned = $("#forms_to_be_cloned form:first").clone(); //get div cloned
      var html = ""; //delare this
      //loop through value from 0 to slected value
      for (var i = 1; i <= selectedValue; i++) {
        html += i + " Form : <br>" + $(cloned).html(); //add htmls inside html variable
    
      }
      $("#populate_here").html(html) //finnaly add the generate html to div
    }
    getSelectedValue();
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select id="cantPreguntas" onchange="getSelectedValue();">
    
      <option value="3">
        3
      </option>
      <option value="4">
        4
      </option>
      <option value="5">
        5
      </option>
      <option value="6">
        6
      </option>
    
    </select>
    <p id='cont'></p>
    <!--keep this display none will be use for cloning-->
    <div id="forms_to_be_cloned" style="display:none">
      <c:forEach begin="1" end="3">
        <form class="form form-goup">
          <label for="txtPregunta">Pregunta num: (num) </label>
          <input type="text" name="txtPregunta" id="txtPregunta">
          <br> Cantidad de respuestas
          <select>
            <c:forEach var="r" begin="2" end="8">
              <option value="${r}">
                <c:out value="${r}"></c:out>
              </option>
            </c:forEach>
          </select> <br> Respuesta (num)<input type="text" name="txtRespuesta" id="txtRespuesta"> <input type="checkbox">
          <br><br><br><br>
        </form>
      </c:forEach>
    </div>
    <div id="populate_here">
    </div>