Prevent deletion of the first line of input

So I fiddled with dynamic input fields and everything works great. The only problem I ran into was stopping the first one input_line

from deleting.

So almost everyone input_line

(see my fiddle for exmaple) should be removed except the first one, which should always remain.

Illustration:

enter image description here

Any suggestions how I can achieve this?

HTML:

<form action="javascript:void(0);" method="POST" autocomplete="off">
    <button class="add">Add Field</button>
    <div class='input_line'>
        <input type="text" name="input_0" placeholder="Input1"><input type="button" class="duplicate" value="duplicate"><input type="button" class="remove" value="remove">
    </div>
</form>

      

JQuery

$(document).ready(function () {
    'use strict';
    var input = 1,
        blank_line = $('.input_line');

    $('.add').click(function () {
        var newElement = blank_line.clone(true).hide();
        $('form').append(newElement);
        $(newElement).slideDown();
    });

    $('form').on('click', '.duplicate', function () {
        $(this).parent().clone().hide().insertAfter($(this).parent().after()).slideDown();
        $('.input_line').last().before($('.add'));
        input = input + 1;
    });

    $('form').on('click', '.remove', function () {
        $(this).parent().slideUp(function () {
            $(this).remove();
        });
        $('.input_line').last().before($('.add'));
        input = input - 1;
    });
});

      

JSFiddle

Any help would be greatly appreciated!

+3


source to share


3 answers


Hide the delete when there is one line and then show it:

http://jsfiddle.net/gqgaswdy/23/



$(document).ready(function () {
     'use strict';
     var input = 1,
          blank_line = $('.input_line');
     var removing = false;
     $('.remove').hide();

     $('.add').click(function () {
          var newElement = blank_line.clone(true).hide();
          $('form').append(newElement);
          $(newElement).slideDown();
          $('.remove').show();
     });

     $('form').on('click', '.duplicate', function () {
          $(this).parent().clone().hide().insertAfter($(this).parent().after()).slideDown();
          $('.input_line').last().before($('.add'));
          $('.remove').show();
          input = input + 1;
     });

     $('form').on('click', '.remove', function () {
          if (removing) {
                return;
          } else {
                if ($('.input_line').length <= 2) $('.remove').hide();
                $(this).parent().slideUp(function () {
                     $(this).remove();
                     removing = false;
                });
                $('.input_line').last().before($('.add'));
                input = input - 1;
          }
          removing = true;
     });
});

      

+3


source


Just wanted to show you how easy it is to create this kind of behavior within the framework actually designed for data binding (which jquery is not there, so you have to write a lot of extra / unnecessary logic) with knockout.js example

Demo



<div data-bind="foreach: rows">
    <div>
    <input type="text" data-bind="value: $data.name">
    <button data-bind="click: $root.duplicate">Duplicate</button>
    <button data-bind="click: $root.remove, enable: $root.rows().length > 1">Remove</button>
    </div>
</div>
<button id="button" data-bind="click: addRow">add Row</button>

var row = function(name) {         
    this.name = ko.observable(name);
}; 

function TableModel() {
    var self = this;
    self.rows = ko.observableArray([]);
    self.addRow = function() {
        self.rows.push( new row('') );
    };
    self.duplicate = function(a) {
        self.rows.push( new row(a.name()) );
    };
    self.remove = function(a) {
        self.rows.remove(a);
    };
}
ko.applyBindings(new TableModel());

      

+1


source


Maybe add a new function like this:

function HideRemoveButton(){
        if($('.input_line').length <= 1){
            $('.remove').hide()
        }
        else{
            $('.remove').show()
        }
    }

      

0


source







All Articles