0

JsFiddle has a nice demo but only works up to jquery 1.8. Here is the link: http://jsfiddle.net/tZPg4/12409/

Add Another Input Box

$(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() { 
        if( i > 2 ) {
            $(this).parents('p').remove();
            i--;
        }
        return false;
    });
});

Version 1.9, it needs Migrate 1.1.0 to be able to have "Remove" work as expected.

Version 1.10 and later won't work for both .live and .on

tanaydin
  • 5,171
  • 28
  • 45

2 Answers2

1

fixed it like this...

$(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;
        });

        $( "body" ).on( "click", "#remScnt", function() {
                if( i > 2 ) {
                       $(this).parents('p').remove();
                       i--;
                }
                return false;
         });
});
tanaydin
  • 5,171
  • 28
  • 45
0

Here is the working code for any version of jQuery:

$(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').length + 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="#" class="remScnt">Remove</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $('#p_scents').on('click', ".remScnt", function() { 
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
});