1

$(document).ready(function() {
  function getData1() {
    var innerhtml1 = $('.load1').html();
    $('.main_div').append(innerhtml1);
  }
  getData1();

  $(document).on('click', '.add_question', function() {
    let lengthQstn = $('.main_div').children().length;
    let questionHtml = $('.load1').html();
    let appendQstn = questionHtml.replace(/0/g, lengthQstn).replace(/add_question/g, "remove_question").replace(/add question/g, "remove question");
    $('.main_div').append(appendQstn);
  })

  $(document).on('click', '.add_option', function() {
    var dataParent = $(this).attr('data-parent');
    let lengthOpt = $("#option_" + dataParent).children().length;
    lengthOpt++;
    var dataParent = $(this).attr('data-parent');
    let optionHtml = $('.load2').html();
    let appendOpt = optionHtml.replace(/count/g, lengthOpt).replace(/0/g, dataParent);
    $("#option_" + dataParent).append(appendOpt);
  })

  $(document).on('click', '.remove_question', function() {
    var dataParent = $(this).attr('data-parent');
    $("#question_" + dataParent).remove();
  })

  $(document).on('click', '.remove_option', function() {
    var dataParent = $(this).attr('data-parent');
    var dataIndex = $(this).attr('data-id');
    $("#row_" + dataParent + '_' + dataIndex).remove();
  })
})
.hide {
  display: none;
}

.remove_option,
.remove_question {
  background-color: rgb(204, 177, 177);
}

.add_question,
.add_option {
  background-color: rgb(201, 233, 222);
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.3/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>

<div class="container">
  <center>
    <h2>New Form</h2>
  </center>
  <div class="main_div">
  </div>
</div>
<div class="load1 hide">
  <div class="parent_div mb-5 mt-5" id="question_0">
    <div class="div1">
      <div class="row">
        <div class="col-6">
          <label id="question_0">Question_0</label>
          <input type="text" name="question_0" id="question_0" class="form-control">
        </div>
        <div class="col-3">
          <button id="add_question_0" class="form-control add_question mt-4" data-parent="0">add_question</button>
        </div>
      </div>
    </div>
    <div class="div2" id="option_0">
      <div class="row" id="row_0_1">
        <div class="col-6">
          <label id="option_0_1">option_0_1</label>
          <input type="text" name="option_0_1" id="option_0_1" class="form-control">
        </div>
        <div class="col-3">
          <button id="add_option_0" class="form-control add_option mt-4" data-parent="0">add
                            option</button>
        </div>
      </div>
      <div class="row" id="row_0_2">
        <div class="col-6">
          <label id="option_0_2">option_0_2</label>
          <input type="text" name="option_0_2" id="option_0_2" class="form-control">
        </div>
      </div>
    </div>
  </div>
</div>
<div class="load2 hide">
  <div class="row" id="row_0_count">
    <div class="col-6 option_0_count">
      <label id="option_0_count">option_0_count</label>
      <input type="text" name="option_0_count" id="option_0_count" class="form-control">
    </div>
    <div class="col-3">
      <button id="remove_option_count" class="form-control remove_option mt-4" data-parent="0" data-id="count">remove option</button>
    </div>
  </div>
</div>

I have made a page for dynamically appending input fields for new questions, as well as new options.

All are input fields only, I have to generate an index for the fields.

But, when I click on "add option" the options input fields are appending but the index is not being generated correctly; also I have to generate an index similarly for the buttons too. Please help with this part.

Also, I have to dynamically remove the added option fields too when clicked on "remove option" button, I have commented it as of now.

16
  • I spent a while watching your code and there are few things to say before addressing the question. You are starting from a question (question+its own options) already existing in dom and at page ready you are populating its captions and ids. Plus you have one button to add an option to that question or to add new question from scratch with its own options. Now said that I could see you create the new questions from an hidden element instead of using templates. Plus you change id using fake ids on html that you replace with js... Commented Jan 21, 2023 at 8:12
  • there's some mess with that approach.. and you also have duplicated it here and there that you shouldn't. The point is that I made so many modifications that I can't even remember how was the original. I'm using template content elements with no ids upfront that I just clone and populate their caption/ids before adding to the dom. But it went too far and maybe it's no more the answer you were expecting. So I'm asking: are you available to a huge refactor that does more or less the same? or you are strictly asking why your code doesn't work? Commented Jan 21, 2023 at 8:17
  • yes i can use a refactor Commented Jan 23, 2023 at 11:02
  • Unfortunately I trashed anything I came out with .. if your comments arrived earlier probably I would have something to post as answer. Commented Jan 23, 2023 at 11:40
  • okay thanks i figured it out, but i have another query, "$(this).parent().parent().parent().remove();" how can i access the element i have to remove without using parent() method but by using id, also i have updated the code above Commented Jan 23, 2023 at 13:01

0

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.