SO - 26059023

by Arun P Johny

HTML

<div id="log"></div>
<button id="add">Add</button>
<div id="inp"></div>

CSS

#log {
    max-height: 100px;
    overflow-y: scroll;
}

JavaScript

var log = (function () {
    var $log = $('#log');
    return function (msg) {
        $('<p/>', {
            text: msg
        }).prependTo($log)
    }
})();

$('document').ready(function () {
    var max = 5;
    var x = 0;

    $('#add').click(function (e) {
        if (x < max) {
            $('#inp').append('<div><input class = "new_input" type=text name="name[]" placeholder="Unesite podatak"/><a class="remove_field "href="#"> X</a><div><br/>');
            x++;
        }

        $('.remove_field').click(function (e) {
            log('remove');
            e.preventDefault();
            $(this).parent('div').remove();
            x--;
            log('x: ' + x)

        })

    });
});