mongoose00318 Posted September 10, 2020 Share Posted September 10, 2020 I'm trying to create a simple form that allows the user to add another item when they click a button. Here is my HTML: <div class="row led-item"> <div class="form-group col-10"> <label for="exampleFormControlSelect1">Product</label> <select class="form-control" id="exampleFormControlSelect1"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> <div class="form-group col-2"> <label for="exampleFormControlInput1">Quantity</label> <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com"> </div> </div> My JS: //add item to request leds forms $('#request-leds-add-item').click(function() { $('.led-item:first').clone().appendTo('.led-item'); }); But instead of it cloning the .led-item and inserting it after the first instance of it; it inserts it into the the first instance of .led-item like so: <div class="row led-item"> <div class="form-group col-10"> <label for="exampleFormControlSelect1">Product</label> <select class="form-control" id="exampleFormControlSelect1"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> <div class="form-group col-2"> <label for="exampleFormControlInput1">Quantity</label> <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com"> </div> <div class="row led-item"> <div class="form-group col-10"> <label for="exampleFormControlSelect1">Product</label> <select class="form-control" id="exampleFormControlSelect1"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> <div class="form-group col-2"> <label for="exampleFormControlInput1">Quantity</label> <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com"> </div> </div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/311457-jquery-clone/ Share on other sites More sharing options...
mongoose00318 Posted September 10, 2020 Author Share Posted September 10, 2020 Kind of have it working now but it's doubling everything up each time instead of just adding one more element : $('.led-item').first().clone().insertAfter('.led-item'); Quote Link to comment https://forums.phpfreaks.com/topic/311457-jquery-clone/#findComment-1581268 Share on other sites More sharing options...
mongoose00318 Posted September 10, 2020 Author Share Posted September 10, 2020 It seems like a selector problem. It doesn't seem to matter if I use .first() or .last() or nothing at all; when I dump it to the console it is selecting all .led-item(s) Quote Link to comment https://forums.phpfreaks.com/topic/311457-jquery-clone/#findComment-1581269 Share on other sites More sharing options...
mongoose00318 Posted September 10, 2020 Author Share Posted September 10, 2020 var template = $('#request_led_dialog .led-item:first').clone(); template.insertAfter('#request_led_dialog .led-item:last'); This seems to have fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/311457-jquery-clone/#findComment-1581272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.