0

I have a page where the fields can be multiple.

At first, my code was working when I only had 1 add and one remove button. When I decided to have a remove button beside every appended field, my jquery function doesn't work anymore.

I've tried adding the 'on', and changing my code to $this.parent().remove() but it's still not working.

Here's my HTML:

   <div class="row" id="ph>
   <span class="section">General Information</span>
       <label> <h3> Project Head </h3> </label>
   </div>


    <div class= "row">
        <div class="col-md-3 col-sm-3 col-xs-12 form-group"></div>
        <div class="col-md-3 col-sm-3 col-xs-12 form-group"></div>
        <button id="addc" type ="button" class ="btn btn-default">Add</button>
   </div>

Here's my code for the append and remove:

$("#ph").append($('<div class="col-md-12 col-sm-12 col-xs-12" id="ne"><div class="row"><div class="col-md-3 col-sm-3 col-xs-12 form-group"> <input class="phname form-control has-feedback-left" name="phname[' + numberIncr + ']" placeholder="Name" type="text"/><span class="fa fa-user form-control-feedback left" aria-hidden="true"></span></div> <div class="col-md-3 col-sm-3 col-xs-12 form-group"><input class="phnum form-control has-feedback-right" name="phnum[' + numberIncr + ']" placeholder="Contact Number" /><span class="fa fa-phone form-control-feedback right" aria-hidden="true"></span></div></div></div>'));

  $("#addc").on('click', function () {
       numberIncr++;
       var temp = $("#ph").append($('<div class="col-md-12 col-sm-12 col-xs-12" id="new"><div class="row"><div class="col-md-3 col-sm-3 col-xs-12 form-group"> <input class="phname form-control has-feedback-left" name="phname[' + numberIncr + ']" placeholder="Name" /><span class="fa fa-user form-control-feedback left" aria-hidden="true"></span></div> <div class="col-md-3 col-sm-3 col-xs-12 form-group"><input class="phnum form-control has-feedback-right" name="phnum[' + numberIncr + ']" placeholder="Contact Number" /><span class="fa fa-phone form-control-feedback right" aria-hidden="true"></span></div><button id="remc" type ="button" class ="btn btn-default">Remove</button></div></div>'));  

      $(".phname").each(function(){
        $(this).rules( "add", {
        required:true,
        lettersonly: true,
      messages: {
        required: 'Enter project head name',
        lettersonly:'Alphabetic characters only'
      }
    });
      });

      $(".phnum").each(function(){
        $(this).rules( "add", {
        required: true,
        maxlength: 11,
        minlength: 11,
      messages: {
        required: 'Enter project head contact number',
        maxlength: 'Invalid contact number',
        minlength: 'Invalid contact number'
      }
    });
      });

    });


 $("#remc").on('click', function () {
        var v= document.getElementById("new");
        v.remove();
  }); 
  • You can't repeat id's in a page, they must be unique by definition. Also will need to use **[event delegation](http://learn.jquery.com/events/event-delegation/)** to account for elements that don't exist at run time – charlietfl Apr 02 '17 at 15:12
  • You need to edit your question, HTML content. Where is the div with id "#ph" ? – edonbajrami Apr 02 '17 at 15:16
  • sorry, this is where the tag is.
    General Information
    – Alex Espiritu Apr 02 '17 at 15:22

0 Answers0