Koen van de Sande
Koen van de Sande

Reputation: 223

Jquery pasting a cloned tr after current div

I'm building a system to automatic create strings. I have some problems with copying and pasting some cloned data. When I press my 'Add Product' button, a tr will be pasted under the last tr in that table with the following code: $('.tableProducts tr').last().after(item); This works fine, as you can see in my example down below:

Situation 0(When you visit the url)
enter image description here Situation 1: Add Product is clicked enter image description here

Now this is my problem: When I click on the Create option button, the table will be cloned so that an other option with products can be created. The add product button in the second option still works, it adds an other tr underneath the second option. BUT when i click the first add product button, it adds an other tr underneath the last option that has been added. It should add a tr in the table that it has been nested in, not the last table! Here are some examples so you get a better feeling about what I mean.

1. Create option has been clicked enter image description here 2. Add Product has been clicked in the second option enter image description here 3. Add Product has been clicked in the first option ( PROBLEM! ) enter image description here

I hope you'll understand my problem, down below you can find my code.

$(document).ready(function() {
  $('.addproduct').click(function(e) {
    // Clone a product and add it to the option.
    e.preventDefault();
    var item = $('.hiddenTemplate tr').eq(1).clone(true);
    $('.tableProducts tr').last().after(item);

  });
  $('.addOption').click(function(e) {
    e.preventDefault();
    var optiondiv = $('.optiondiv:first').clone(true);
    var productdiv = $('.productdiv:first').clone(true);

    var optiontable = $('.optiondiv table:eq(1)').clone(true);
    var producttable = $('.productdiv table:eq(1)').clone(true);

    optiondiv.html(optiontable);
    productdiv.html(producttable);

    optiontable.removeAttr('class');
    producttable.removeAttr('class');

    optiontable.addClass('tableOptions');
    producttable.addClass('tableProducts');

    $('.hr').before(optiondiv);
    $('.hr').before(productdiv);

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
EDIT 1: HTML ADDED

<div class="container">
  <div class="optiondiv">
    <table class="tableOptions">
      <tbody>
        <tr>
          <td><b>Option</b>:</td>
          <td><b>Title</b>:</td>
          <td><b>Position</b>: </td>
          <td><b>Input Type</b>:</td>
          <td><b></b></td>
        </tr>
        <tr>
          <td>
            <button class="addproduct">Add Product</button>
          </td>
          <td>
            <input type="text" id="title">
          </td>

          <td>
            <input type="text" id="position">
          </td>
          <td>
            <select id="input">
                        <option value="1">Drop-down</option>
                        <option value="2">Radio Buttons</option>
                        <option value="3">Checkbox</option>
                        <option value="4">Multiple Select</option>
                    </select>
          </td>
          <td>
            <input type="checkbox" id="required"> Required?
          </td>
        </tr>
      </tbody>
    </table>
    <table class="hiddenOption">
      <tbody>
        <tr>
          <td><b>Option</b>:</td>
          <td><b>Title</b>:</td>
          <td><b>Position</b>: </td>
          <td><b>Input Type</b>:</td>
          <td><b></b></td>
          <td><b></b></td>
        </tr>
        <tr>
          <td>
            <button class="addproduct">Add Product</button>
          </td>
          <td>
            <input type="text" id="title">
          </td>

          <td>
            <input type="text" id="position">
          </td>
          <td>
            <select id="input">
                        <option value="1">Drop-down</option>
                        <option value="2">Radio Buttons</option>
                        <option value="3">Checkbox</option>
                        <option value="4">Multiple Select</option>
                    </select>
          </td>
          <td>
            <input type="checkbox" id="required"> Required?
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <div class="productdiv">
    <table class="tableProducts">
      <tbody>
        <tr>
          <td><b>SKU</b>:</td>
          <td><b>Default Quantity</b>:</td>
          <td><b>Position</b>:</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>
            <input type="text" id="sku">
          </td>
          <td>
            <input type="text" id="quantity">
          </td>
          <td>
            <input type="text" id="position">
          </td>
          <td>
            <input type="radio" id="isDefault"> Is default?
          </td>
          <td>
            <input type="checkbox" id="userdefined"> User required?
          </td>
        </tr>
      </tbody>
    </table>
    <table class="hiddenTemplate">
      <tbody>
        <tr>
          <td><b>SKU</b>:</td>
          <td><b>Default Quantity</b>:</td>
          <td><b>Position</b>:</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>
            <input type="text" id="sku">
          </td>
          <td>
            <input type="text" id="quantity">
          </td>
          <td>
            <input type="text" id="position">
          </td>
          <td>
            <input type="radio" id="isDefault"> Is default?
          </td>
          <td>
            <input type="checkbox" id="userdefined"> User required?
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <hr class="hr">
  <button class="addOption">Create option</button>
</div>

If you need some more explanation or code(html), feel free to ask me! Happy coding

Upvotes: 3

Views: 168

Answers (2)

Vara Prasad
Vara Prasad

Reputation: 497

You can try this code as per your table structure

$('.addproduct').click(function(e) {
    // Clone a product and add it to the option.
    e.preventDefault();
    var item = $('.hiddenTemplate tr').eq(1).clone(true);
    $(this).parents(".optiondiv").next().find(".tableProducts tr").last().after(item);

  });

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

You need to get the parent div and find the table inside it otherwise it will always add in the last found table so your code should be like,

$('.addproduct').click(function (e) {
    // Clone a product and add it to the option.
    e.preventDefault();
    var $parent = $(this).closest('.productdiv'), // find parent div
        item = $parent.find('.hiddenTemplate tr').eq(1).clone(true);
    $parent.find('.tableProducts tr').last().after(item);
});

Updated, as per your HTML structure you need something like this,

$('.container').on('click','.addproduct',function(e) {
    // Clone a product and add it to the option.
    e.preventDefault();
    var $parent = $(this).closest('.optiondiv').next('.productdiv'), // find parent div
        item = $('.hiddenTemplate tr').eq(1).clone(true);
    $parent.find('.tableProducts tr').last().after(item);
});

$(document).ready(function() {
  $('.container').on('click','.addproduct',function(e) {
    // Clone a product and add it to the option.
    e.preventDefault();
    var $parent = $(this).closest('.optiondiv').next('.productdiv'), // find parent div
      item = $('.hiddenTemplate tr').eq(1).clone(true);
    $parent.find('.tableProducts tr').last().after(item);
  });
  $('.addOption').click(function(e) {
    e.preventDefault();
    var optiondiv = $('.optiondiv:first').clone(true);
    var productdiv = $('.productdiv:first').clone(true);

    var optiontable = $('.optiondiv table:eq(1)').clone(true);
    var producttable = $('.productdiv table:eq(1)').clone(true);

    optiondiv.html(optiontable);
    productdiv.html(producttable);

    optiontable.removeAttr('class');
    producttable.removeAttr('class');

    optiontable.addClass('tableOptions');
    producttable.addClass('tableProducts');

    $('.hr').before(optiondiv);
    $('.hr').before(productdiv);

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container">
  <div class="optiondiv">
    <table class="tableOptions">
      <tbody>
        <tr>
          <td><b>Option</b>:</td>
          <td><b>Title</b>:</td>
          <td><b>Position</b>: </td>
          <td><b>Input Type</b>:</td>
          <td><b></b></td>
        </tr>
        <tr>
          <td>
            <button class="addproduct">Add Product</button>
          </td>
          <td>
            <input type="text" id="title">
          </td>

          <td>
            <input type="text" id="position">
          </td>
          <td>
            <select id="input">
                    <option value="1">Drop-down</option>
                    <option value="2">Radio Buttons</option>
                    <option value="3">Checkbox</option>
                    <option value="4">Multiple Select</option>
                </select>
          </td>
          <td>
            <input type="checkbox" id="required"> Required?
          </td>
        </tr>
      </tbody>
    </table>
    <table class="hiddenOption">
      <tbody>
        <tr>
          <td><b>Option</b>:</td>
          <td><b>Title</b>:</td>
          <td><b>Position</b>: </td>
          <td><b>Input Type</b>:</td>
          <td><b></b></td>
          <td><b></b></td>
        </tr>
        <tr>
          <td>
            <button class="addproduct">Add Product</button>
          </td>
          <td>
            <input type="text" id="title">
          </td>

          <td>
            <input type="text" id="position">
          </td>
          <td>
            <select id="input">
                    <option value="1">Drop-down</option>
                    <option value="2">Radio Buttons</option>
                    <option value="3">Checkbox</option>
                    <option value="4">Multiple Select</option>
                </select>
          </td>
          <td>
            <input type="checkbox" id="required"> Required?
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <div class="productdiv">
    <table class="tableProducts">
      <tbody>
        <tr>
          <td><b>SKU</b>:</td>
          <td><b>Default Quantity</b>:</td>
          <td><b>Position</b>:</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>
            <input type="text" id="sku">
          </td>
          <td>
            <input type="text" id="quantity">
          </td>
          <td>
            <input type="text" id="position">
          </td>
          <td>
            <input type="radio" id="isDefault"> Is default?
          </td>
          <td>
            <input type="checkbox" id="userdefined"> User required?
          </td>
        </tr>
      </tbody>
    </table>
    <table class="hiddenTemplate">
      <tbody>
        <tr>
          <td><b>SKU</b>:</td>
          <td><b>Default Quantity</b>:</td>
          <td><b>Position</b>:</td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td>
            <input type="text" id="sku">
          </td>
          <td>
            <input type="text" id="quantity">
          </td>
          <td>
            <input type="text" id="position">
          </td>
          <td>
            <input type="radio" id="isDefault"> Is default?
          </td>
          <td>
            <input type="checkbox" id="userdefined"> User required?
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <hr class="hr">
  <button class="addOption">Create option</button>

Upvotes: 1

Related Questions