0

Input fields are added dynamically but unable to remove the fields added . i want to add input dynamically using jquery and remove it .Add function is working perfectly . but there is problem in remove function

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <style>
    * { font-family:Arial; }
    h2 { padding:0 0 5px 5px; }
    h2 a { color: #224f99; }
    a { color:#999; text-decoration: none; }
    a:hover { color:#802727; }
    p { padding:0 0 5px 0; }
    input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }
    </style>
    <h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
    <div id="p_scents">
        <p>
            <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
        </p>
    </div>
    <script>
    $(function() {
            var scntDiv = $('#p_scents');
            var i = $('#p_scents p').size() + 1;

            $('#addScnt').on('click', function() {

                   $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
                    i++;
                    return false;
            });

            $('#remScnt').on('click', function() { 
                 //alert('ok');
                 if( i > 2 ) {
                            $(this).parents('p').remove();
                            i--;
                    }
                    return false;
            });
    });
    </script>
Raman
  • 247
  • 2
  • 10

0 Answers0