2

I am trying to have some inputs for my users, and I want them to be able to hit a button to add another row of inputs. I am trying to use jQuery to add a list of inputs after the first.

Here is what I have tried so far:

$("#addButton").on("click", function() {
  $("#inputs").append($("#linear"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inputs">
  <ul class="linearFeetCalc" id="linear">
    <li><input type="number" placeholder="Quantity" id="quantity"></li>
    <li><input type="number" placeholder="Length" id="length"></li>
    <li><input type="number" placeholder="Width" id="width"></li>
    <li><input type="number" placeholder="Height" id="height"></li>
    <li><input type="checkbox" id="stackable"> Stackable?</li>
    <li><input type="checkbox" id="turnable"> Turnable?</li>
  </ul>
</div>
<button id="addButton">Add Item</button>

1 Answer 1

2

Very close. you just need to clone it first:

$("#addButton").on("click", function() {
    $("#inputs").append($("#linear").clone());
});

Edit: You should also change the ID to prevent duplicates in your DOM:

`...$("#linear").clone().prop('id', '#linear2')...`
Sign up to request clarification or add additional context in comments.

2 Comments

Also the input elements inside the list will get duplicated with duplicate ids. Will that will not a problem ? @George
Oh the rabbit hole goes deeper!

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.