0

My html like this :

<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>

My javascript like this :

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

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

My demo and full script like this : http://jsfiddle.net/4nvc0jmb/

If I click add input text then it works, but if I click remove input text, it does not work. I check on the console, but there is no error

How can I solve this problem?

Success Man
  • 5,513
  • 23
  • 109
  • 205
  • Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Shree May 26 '19 at 11:00

1 Answers1

1

Element-ids should be unique, so better use the class or an other property to identify your elements (if you have multiple).

You also need to register the event-lsitener on an object that is already in the document. and filter that event-listener with the kind of objects you want to use this event-listener... otherwise the event-lsitener is only available for objects that are present at time of registering that event-listener.

What i changed to make it run:

  • I changed the button id to class.
  • Registered the event-listener on the document-element, and filtered for that button-class.

$(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;
  });
        
  $(document).on('click', '.remScnt', function() { 
    if( i > 2 ) {
      $(this).parents('p').remove();
      i--;
    }
    return false;
  });
  
});
* { 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; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<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>
Jan
  • 2,744
  • 2
  • 18
  • 25