1

I want to create another input field, everytime the button "+" inside the form is clicked. I searched for a solution but i can't find anything. The input field should be created inside the form over the "+" button.

Here is my current code:

<div class="row">
        <div class="col-md-12">
          <form method="post" id="create-form" action="create.php" enctype="multipart/form-data">
            <?php
            /* getting sample questions */
            /* checks result and creates variables from result */

            if ($sampleamount > 0) { // amount
              $samplequery->bind_result($questionid_sample, $question_sample);
              while ($samplequery->fetch()) { // while page can use this variables
                /* echo text-box, value from db */
                $required = $questionid_sample === 1 ? "required" : ""; // one question is always required
                echo "<input class='question-box' type='text' name='" . $questionid_sample . "' placeholder='Write your question in here.' maxlength='255' size='70' value='" . $question_sample . "'" . $required . "> <br>";
              }
            } else {
              /* no result (db=sample_question) */
              header('Location: ../index.php');
            }

            $samplequery->close();
            closeDB($conn);

            /* adds more input for user */
            for ($i = ($sampleamount + 1); $i <= ($additionalquestions + $sampleamount); $i++) {
              echo "<input class='question-box' type='text' name='" . $i . "' placeholder='Write your question in here.' maxlength='255' size='70'> <br>";
            }
            ?>
            <br>
            <input class="createsurvey2" type="button" id="addqbtn" name="addqbtn" value="+" >
            <br>
            <input class="createsurvey2" type="submit" name="btnSubmit" value="Create Survey!" >
          </form>
        </div>
      </div>
    </div>
    <script>
            function addquestion() {
              var counter = 2;
              var addqbtn = document.getElementById('addqbtn');
              var form = document.getElementById('create-form');
              var addInput = function() {
                counter++;
                var input = document.createElement("input");
                input.id = 'additionalquestion-' + counter;
                input.type = 'text';
                input.class = 'question-box';
                input.name = 'name';
                input.placeholder = 'Write your question in here.';
                form.appendChild(input);
              };
              addqbtn.addEventListener('click', function(){
                addInput();
              }.bind(this));
            };
          </script>
1

1 Answer 1

0
<html>
<body>
<button onclick="createNewFiled()" id="incrementBtn" type="button">Click here</button>
    <form id="form" action="">
    </form>
</body>
</html>

<script type="text/javascript">

    var counter = 0;
    var incrementBtn = document.getElementById('incrementBtn');
    var form = document.getElementById('form');

    var createNewFiled = function() {
        counter++;

        var input = document.createElement("input");
        var br = document.createElement("br");  // If you need new line after each input field

        input.id = 'input-' + counter;
        input.classList.add("inputClass");
        input.type = 'text';
        input.name = 'name'+counter;
        input.placeholder = 'Input field ' + counter;

        form.appendChild(input);
        form.appendChild(br);  // If you need new line after each input field
    };
    
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

it now creates an input field, finally. But i need to give a class name to the new created input to look like the input fields above and it also creates the input fields below the "+" button, but it should add them above. Is it possible to do that?
class name now works, but it still adds the inputs below the button, does anyone know how to fix it?
Edited the answer according to your need. Please mark the answer accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.