Jquery Button Add Field From Input

Now I have example code to create dinamic form input, example. If we click add field button, so we can get new field input. For more details, see this example code. The code I got from my friend.

<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="<?=base_url("assets/js/jquery-1.8.2.min.js");?>"></script>
    <script type="text/javascript">
    function addField() {
            var $row = $($('#template').html());
            $('#field_grid tbody').append($row);

            $('#field_grid').find("tr:nth-child(odd)").removeClass('even').removeClass('odd').addClass("odd");
            $('#field_grid').find("tr:nth-child(even)").removeClass('even').removeClass('odd').addClass("even");
        }

    function removeField($o) {
        if ($o.parents('tbody').find('tr').length <= 2) {
            addField();
        }
        $o.parents('tr').remove();
    }

    $('#add_field').live('click', function(evt) {
        addField();
        return evt.preventDefault(); 
    });

    $('.btn-remove').live('click', function(evt) {
        removeField($(this));
        return evt.preventDefault();
    });

    $('.btn-up').live('click', function(evt) {
        var current = $(this).parents('tr');
        current.prev().before(current);
        return evt.preventDefault();
    });

    $('.btn-down').live('click', function(evt) {
        var current = $(this).parents('tr');
        current.next().after(current);
        return evt.preventDefault();
    });
    </script>
</head>
<body>
<fieldset>
    <legend>Test</legend>
    <div id="field">
        <table id="field_grid">
            <tr class="odd">
                <th>One</th>
                <th>Two</th>
                <th>Three</th>
            </tr>
            <tr>
                <td>
                    <input type="text" value=""  name="one" placeholder="One" />
                </td>
                <td>
                    <input type="text" value=""  name="two" placeholder="Two" />
                </td>
                <td>
                    <a href="#" class="btn-remove">Delete</a>
                    <a href="#" class="btn-up">Up</a>
                    <a href="#" class="btn-down">Down</a>
                </td>
        </tr>
        </table>
    </div>
    <div class="action-buttons btn-group">
        <input type="submit" class="btn btn-primary" />
        <button id="add_field">Add Field</button>
    </div>
</fieldset>
<script type="text/template" id="template">
    <tr>
        <td>
            <input type="text" value=""  name="one" placeholder="One" />
        </td>
        <td>
            <input type="text" value=""  name="two" placeholder="Two" />
        </td>
        <td>
            <a href="#" class="btn-remove">Delete</a>
            <a href="#" class="btn-up">Up</a>
            <a href="#" class="btn-down">Down</a>
        </td>
    </tr>
</script>
</body>
</html>

Updated

If use jquery with version 2.x, so change the code like this:

<script type="text/javascript">
        function addField() {
            $($('#template').html()).appendTo('#field_grid tbody');
        }
 
        function removeField(e) {
            if($('input[type=text]').parents('tbody').find('tr').length <= 2) {
                return false;
            }
            var row = $(e).closest('tr').remove();
        }
 
        $(function() {
            $('#field_grid').on('click', '.btn-remove', function(e) {
                removeField(e.target);
                return e.preventDefault();
            });
 
            $('#add_field').click(function(e) {
                addField();
                return e.preventDefault();
            });
 
            $('#field_grid').on('click', '.btn-up', function(e) {
                var current = $(this).parents('tr');
                 
                if(current.prevAll().length == 1) {
                    return e.preventDefault();
                }
                current.prev().before(current);
                return e.preventDefault();
            });
 
            $('#field_grid').on('click', '.btn-down', function(e) {
                var current = $(this).parents('tr');
                current.next().after(current);
                return e.preventDefault();
            });
        });
    </script>

Example online: http://jsfiddle.net/imron02/6udka/embedded/result/ And http://jsfiddle.net/imron02/6udka/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s