0

When you click on the buttons + Добавить место работы and + Добавить учебное заведение this object should be copied and appear for information input. 1 click = 1 clone. But I get a lot of clones. What was I wrong about? http://test.bikstart.ru/niz-vac/index2.html

$('.add-study').on('click', function() {
  $('.table-vac--study').clone([true]).appendTo(".form__item__study");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form__item form__item__study">
  <div class="form__item__wrap">
    <img src="img/numer-quest.png" alt="">
    <!--<span>1</span>-->
    <div class="form__wrap">
      <h2 class="h2-quest">Образование</h2>
      <span>Укажите учебные заведения, в которых Вы учились или учитесь</span>
    </div>
  </div>

  <table class='table-vac table-vac--study table-vac-none'>
    <tr class='tr-vac'>
      <td class='td-vac quest-title'>Учебное заведение</td>
      <td class='td-vac'><input type="text" placeholder="Учебное заведение" required id="study"></td>
    </tr>

    <tr class='tr-vac'>
      <td class='td-vac quest-title'><span class='star'>*</span> Специальность</td>
      <td class='td-vac'><input type="text" placeholder="Специальность" required id="specialty"></td>
    </tr>

    <tr class='tr-vac'>
      <td class='td-vac quest-title'><span class='star'>*</span> Год окончания</td>
      <td class='td-vac'><input type="number" required id="year-ending"></td>
    </tr>

    <tr class='tr-vac'>
      <td></td>
      <td class="add-study">+ Добавить учебное заведение</td>
    </tr>
  </table>


  <div class="pseudo-table">
    <div class="pseudo-table__item">
      <pseudo-table-title>Учебное заведение</pseudo-table-title>
      <input type="text" placeholder="Учебное заведение" required id="study1">
    </div>

    <div class="pseudo-table__item">
      <pseudo-table-title><span class='star'>*</span> Специальность</pseudo-table-title>
      <input type="text" placeholder="Специальность" required id="specialty1">
    </div>

    <div class="pseudo-table__item">
      <pseudo-table-title><span class='star'>*</span> Год окончания</pseudo-table-title>
      <input type="number" required id="year-ending1">
    </div>
  </div>

  <!--<span class="add-study">+ Добавить учебное заведение</span>-->
</div>

5
  • After your first clone and append, how many elements with that class are now in the document? Two. So when you click again, how many are you now cloning? Two, resulting in four... and so on. Commented Jan 10, 2018 at 20:50
  • Add console.log($('.table-vac--study').length); to your click handler. My guess is that you have multiple elements with that classname... Commented Jan 10, 2018 at 20:50
  • Cant se anything wrong but try this instead $(this).parent().children('.table-vac--study:first').clone([true]).appendTo(".form__item__study"); Commented Jan 10, 2018 at 20:51
  • @rockstar, yes,it's true Commented Jan 10, 2018 at 21:06
  • @War10ck,no, only one Commented Jan 10, 2018 at 21:08

1 Answer 1

1

Your problem is that you have multiple elements with the class table-vac--study after you have added the first clone to the DOM:

$('.table-vac--study')  //this gets all elements with the class

If you only want one element with the class, you need to specify what element in the class you want.

If you want to the closest element with the class table-vac--study, you can use DOM traversal:

$(this).closest('.table-vac--study').clone([true]).appendTo(".form__item__study");

If you always want the first element with the class:

$('.table-vac--study:first').clone([true]).appendTo(".form__item__study");
Sign up to request clarification or add additional context in comments.

1 Comment

$('.table-vac--study:first').clone([true]).appendTo(".form__item__study"); - it's true for me. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.