0

I am trying to create a form where users can click a plus button to add another row of fields and a remove button. This is what I have so far:

$(".add").click(function() {
    $(".frm > div:first-child").clone(true).insertBefore(".frm > div:last-child");
    return false;
});
        
$(".remove").click(function() {
    $(this).parent().remove();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<form class="frm">
        <div>
            <div class="form-group col-md-1">
                <br/>
                <h4 style="text-align:right">1.</h4>
            </div>
            <div class="form-group col-md-1">
                <label for="title" class="control-label">Title</label>
                <input type="text" value='' class="form-control" id="title" placeholder="Title"/>
            </div>
            <div class="form-group col-md-1">
                <label for="fname" class="control-label">First&nbsp;Name</label>
                <input type="text" value='' class="form-control" id="fname" placeholder="First Name"/>
            </div>
            <div class="form-group col-md-2">
                <label for="sname" class="control-label">Surname</label>
                <input type="text" value='' class="form-control" id="sname" placeholder="Surname"/>
            </div>
            <div class="form-group col-md-2">
                <label for="job" class="control-label">Job</label>
                <input type="text" value='' class="form-control" id="job" placeholder="Job"/>
            </div>
            <div class="form-group col-md-2">
                <label for="class" class="control-label">Class</label>
                <input type="text" value='' class="form-control" id="class" placeholder="Class"/>
            </div>
            <div class="form-group col-md-2 col-md-inset-1">
                <label for="emailadd" class="control-label">Email&nbsp;Address</label>
                <input type="email" value='' class="form-control" id="emailadd" placeholder="Email Address"/>
            </div>
            <span class="remove">Remove</span>      
        </div>
    
        <div>
            <span class="add">Add fields</span>
        </div>
    </form>

With the code above I can add the rows but for some reason I cannot remove them. Also when I do add them the layout gets ruined, instead of the rows appearing underneath each other they append to the end and then break off to another line. what could be causing the remove to not work?

COUNTER JS

 $(document).on('click', '.add', function() {
    $('.counter').html(function(i, val) { return val*1+1 });
});


    $(document).on('click', '.remove', function() {
    $('.counter').html(function(i, val) { return val*1-1 });
});

1 Answers1

0

If you want to add fields even when there are no fields, then one option is a hidden field with the full code.

jsfiddle fullscreen demo (code)

$(document).ready(function() {
    $(".add").click(function() {
        $(".cloneDefault").clone(true).insertBefore(".frm > div:last-child");
        $(".frm > .cloneDefault").removeClass("cloneDefault");
        return false;
   
.cloneDefault{
    display: none;
}
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<div class="row cloneDefault">
    <div class="form-group col-md-1">
        <br/>
        <h4 style="text-align:right">1.</h4>
    </div>
    <div class="form-group col-md-1">
        <label for="title" class="control-label">Title</label>
        <input type="text" value='' class="form-control" id="title" placeholder="Title"/>
    </div>
    <div class="form-group col-md-1">
        <label for="fname" class="control-label">First&nbsp;Name</label>
        <input type="text" value='' class="form-control" id="fname" placeholder="First Name"/>
    </div>
    <div class="form-group col-md-2">
        <label for="sname" class="control-label">Surname</label>
        <input type="text" value='' class="form-control" id="sname" placeholder="Surname"/>
    </div>
    <div class="form-group col-md-2">
        <label for="job" class="control-label">Job</label>
        <input type="text" value='' class="form-control" id="job" placeholder="Job"/>
    </div>
    <div class="form-group col-md-2">
        <label for="class" class="control-label">Class</label>
        <input type="text" value='' class="form-control" id="class" placeholder="Class"/>
    </div>
    <div class="form-group col-md-2 col-md-inset-1">
        <label for="emailadd" class="control-label">Email&nbsp;Address</label>
        <input type="email" value='' class="form-control" id="emailadd" placeholder="Email Address"/>
    </div>
    <span class="remove">Remove</span>      
</div>



<form class="frm">
    <div class="row">
        <div class="form-group col-md-1">
            <br/>
            <h4 style="text-align:right">1.</h4>
        </div>
        <div class="form-group col-md-1">
            <label for="title" class="control-label">Title</label>
            <input type="text" value='' class="form-control" id="title" placeholder="Title"/>
        </div>
        <div class="form-group col-md-1">
            <label for="fname" class="control-label">First&nbsp;Name</label>
            <input type="text" value='' class="form-control" id="fname" placeholder="First Name"/>
        </div>
        <div class="form-group col-md-2">
            <label for="sname" class="control-label">Surname</label>
            <input type="text" value='' class="form-control" id="sname" placeholder="Surname"/>
        </div>
        <div class="form-group col-md-2">
            <label for="job" class="control-label">Job</label>
            <input type="text" value='' class="form-control" id="job" placeholder="Job"/>
        </div>
        <div class="form-group col-md-2">
            <label for="class" class="control-label">Class</label>
            <input type="text" value='' class="form-control" id="class" placeholder="Class"/>
        </div>
        <div class="form-group col-md-2 col-md-inset-1">
            <label for="emailadd" class="control-label">Email&nbsp;Address</label>
            <input type="email" value='' class="form-control" id="emailadd" placeholder="Email Address"/>
        </div>
        <span class="remove">Remove</span>      
    </div>

    <div>
        <span class="add">Add fields</span>
    </div>
</form>  
turbopipp
  • 1,245
  • 2
  • 14
  • 27
  • This worked! Thanks Also another quick question how can i change the value 1. each time a new field is added? so the second one will be numbered 2..3 etc I also want to update a field with a price, so each row will have a standard value set to it and clicking add will total up the value. –  Oct 26 '15 at 15:21
  • I managed to get the total value to work. however the row number doest seem to work properly, all the row numbers change. check update for my javascript –  Oct 26 '15 at 15:56
  • You are kind of asking a whole new question now. My advice is that you make a new question to give it more attention, and give you an answer faster. Alternatively check out this post http://stackoverflow.com/questions/8018132/jquery-clone-form-fields-and-increment-id – turbopipp Oct 26 '15 at 17:27