0

Basically I've had a headache trying to solve this, I want to create a few controls with JS (using jquery) but I also want to add a remove button for them, my attempt is below: html:

                    <div class=col-lg-6>
                        <h1>Pieces</h1>
                        <form method=post>
                        <div id=pieces></div>

                        <button class='btn btn-success' id=addpiece>Add Piece</button>
                        <button class='btn btn-primary'>Submit</button>
                        </form>
                    </div>

And the JS:

                $(document).ready(function(){
                 var counter = 1;
                    $("#addpiece").click(function (e) {

                        $("#pieces").append('<label>Piece ' + counter + ': </label><br>' +
                              '<p id=added'+counter+'><div class=row><div class=col-lg-2><label>Dimensions:</label></div>' + 
                              '<div class=col-lg-10><input min=0 step=0.1 style=width:70px; type="number" name="width' + 
                              counter + '" required>cm x ' +
                              '<input min=0 step=0.1 style=width:70px; type="number" name="height' + 
                              counter + '" required>cm x ' +
                              '<input min=0 step=0.1 style=width:70px; type="number" name="length' + 
                              counter + '" required>cm</div></div>' +
                              '<div class=row><div class=col-lg-2><label>Weight:</label></div>' + 
                              '<div class=col-lg-10><input min=0.001 step=0.001 style=width:70px; type="number" name="weight' + 
                              counter + '" required>KG<br>' +
                              '<input type=hidden name=counter value=' + 
                              counter + '><button class="btn btn-danger" id="remove' + counter + '">X</button>\<script type="text\/javascript"\>' +
                              '$("#remove' + counter + '").click(function (e) { $( "#added'+counter+'" ).remove(); return false; }  \<\/script\>' +
                              '</div></div></p><br>');
                        counter++;
                        return false;
                     });


                  });

Check out my fiddle for clearer view: http://jsfiddle.net/c1Lv7ska/

1 Answers1

0

Since you are dynamically creating the remove button, use event delegation to register the remove handler.

Also it is better if you have a container element which will contain all the elements for a piece, like the one I added(<div class="piece">) to wrap all the elements.

$(document).ready(function () {
    var counter = 1;
    $("#addpiece").click(function (e) {
        $("#pieces").append('<div class="piece"><label>Piece ' + counter + ': </label><br>' + '<p id=added' + counter + '><div class=row><div class=col-lg-2><label>Dimensions:</label></div>' + '<div class=col-lg-10><input min=0 step=0.1 style=width:70px; type="number" name="width' + counter + '" required>cm x ' + '<input min=0 step=0.1 style=width:70px; type="number" name="height' + counter + '" required>cm x ' + '<input min=0 step=0.1 style=width:70px; type="number" name="length' + counter + '" required>cm</div></div>' + '<div class=row><div class=col-lg-2><label>Weight:</label></div>' + '<div class=col-lg-10><input min=0.001 step=0.001 style=width:70px; type="number" name="weight' + counter + '" required>KG<br>' + '<input type=hidden name=counter value=' + counter + '><button type="button" class="btn btn-danger remove" id="remove' + counter + '">X</button>' + '</div></div></p><br></div>');
        counter++;
        return false;
    });
    $("#pieces").on('click', '.remove', function(){
        $(this).closest('.piece').remove()
    })

});

Also read

Demo: Fiddle

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • nice one, also changed to fix the counter issue that was going to come up ^.^ http://jsfiddle.net/99gvgv94/2/ – Javi Qualms Pdog Feb 12 '15 at 09:52
  • another question I'm trying to disable the go button if counter is 0, confused why its not working? http://jsfiddle.net/99gvgv94/13/ – Javi Qualms Pdog Feb 12 '15 at 09:57
  • @JaviQualmsPdog you are starting the counter from 1 so http://jsfiddle.net/arunpjohny/99gvgv94/14/ – Arun P Johny Feb 12 '15 at 10:02
  • thanks but opted out of doing it, more trouble than its worth, e.g if i delete node 1 then adding a button ill have 2 x whatever node im on (if that makes sense lol) could create array and remove each node from array and check if array empty for disabling button but just not worth it as its not important. – Javi Qualms Pdog Feb 12 '15 at 10:09