1

I have this code to automatically fill up phrases below as you type inside the text boxes.

If you click "add person", it will append text boxes and a phrase for the new person. You can type their info and it will still automatically fill up new phrases.

I added also a remove button for each new added person. It removes the text boxes and the phrase below for the respective person.

As you will see, there is a problem: when you click "remove", it won't delete "Bye, {name}"

I have commented my code so you can help me. How do I select the "third appended element" ("Bye, {name}") to remove it too?

 var name1 = document.getElementById('name');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.one');
   result.innerHTML = this.value;
 });
 
  var name1 = document.getElementById('name');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.four');
   result.innerHTML = this.value;
 });

 var name1 = document.getElementById('city');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.two');
   result.innerHTML = this.value;
 });

 var name1 = document.getElementById('age');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.three');
   result.innerHTML = this.value;
 });

 $(document).ready(function() {
   var max_fields = 5; //maximum input boxes allowed
   var wrapper = $(".input_fields_wrap"); //Fields wrapper
   var add_button = $(".add_field_button"); //Add button ID
   var input_container = $(".container"); // holds all input containers

   var x = 1; //initlal text box count

   $(add_button).click(function(e) { //on add input button click
     e.preventDefault();
     if (x < max_fields) { //max input box allowed
       x++; //text box increment
       
       /* FIRST APPENDED ELEMENT */
       
       $(wrapper).append("<div class='container" + x + "' ><label>Name</label><input type='text'                            class= 'name' name='mytext[]'><a href='#' class='remove_field'>Remove</a><br><label>City</label><input type='text'                            class= 'city' name='mytext[]'><br><label>Age</label><input type='text'                            class= 'age' name='mytext[]'></div>");
       
       /* SECOND APPENDED ELEMENT */
       
       $('div.hello').append('<div id="container' + x + '" id = "' + x + '"   >Hello, <span class="name"></span> <span> ,from</span> <span class="city"></span> <span> Your age is:</span><span class="age"> </span><br><br></div>');
       
       /* THIRD APPENDED ELEMENT */
       
       $('div.hello').append('<div id="container' + x + '" id = "' + x + '"   >Bye, <span class="name"></span><br><br></div>');

       $('input').on('input', function(e) {

         var parcontainer = $(this).parents('div').attr('class');


         spantoappend = $(this).attr('class');
         var val = $(this).val();
         var sel = "#" + parcontainer + '  .' + spantoappend;
         //  var sel = "#" + divtoappend + " span";
         $(sel).text('');
         $(sel).append(val);
       });

     }
   });

   $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
     e.preventDefault();
     
     /* THIS REMOVES SECOND APPENDED ELEMENT */ /* HOW DO I MAKE THIS REMOVE THIRD APPENDED ELEMENT TOO? */
     
     var rem = $(this).parents('div').attr('class');
     $('#' + rem).remove();
     
      /* THIS REMOVES FIRST APPENDED ELEMENT */ 
     
     $(this).parent('div').remove();
     x--;

   });


 });
.block {
  display: block;
}

input {
  width: 50%;
  display: inline-block;
  margin: 4px;
  margin-left: 10px;
}

span.add,
span.remove {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}

label {
  display: inline-block;
  width: 40px;
  text-align: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input_fields_wrap">
  <button class="add_field_button">Add person</button>

  <br><br>
  <div class="container">
    <label>Name</label><input type="text" id="name" name="mytext[]"> <br>
    <label>City</label><input type="text" id="city" name="mytext[]"> <br>
    <label>Age</label><input type="text" id="age" name="mytext[]">
  </div>
</div>
<br>
<div class="hello">

  Hello, <span class="one"></span>, from <span class="two"></span>. Your age is: <span class="three"></span><br><br>

 Bye, <span class="four"></span><br><br>

</div>

<div class="bye">

   

</div>
Rohalt
  • 147
  • 1
  • 2
  • 13
  • 2
    You're using the same `id='container"+x` for multple DIVs. IDs need to be unique. Use a class instead. – Barmar Apr 21 '18 at 21:42
  • @Rohalt check [this](https://stackoverflow.com/a/49960572/6804958) answer – Muhammad Usman Apr 21 '18 at 22:01
  • @Barmar I tried changing `
    ` for `
    ` on the second and third appended elemenets. It worked great. **Just one problem**: when you type in the new text box, it won't let you type more than 1 or 2 characters (see: https://jsfiddle.net/dyLoxddx/2/ ). You have to click the input again to type 1 or 2 characters more, and then the problem happens again
    – Rohalt Apr 23 '18 at 00:19
  • 1
    In your fiddle, `sel` is something like `.container2 .name`. This matches both the input field you're typing into and the spans you want to copy the input to. Doing `$(sel).text(''); $(sel).append(val);` seems to remove focus from the input field. You should probably make it `.container2 span.name2`. – Barmar Apr 23 '18 at 16:49
  • Also, each time you add a new set of fields, you're doing `$('input').on('input', ...)`. This adds the handler to *all* inputs, not just the ones you just added. See [Event binding on dynamically-created elements](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) for the proper way to do this. – Barmar Apr 23 '18 at 16:51
  • Nicee. Thanks a lot – Rohalt Apr 23 '18 at 17:01

1 Answers1

0

Well, this is how I would've done it.

Assign an ID to div containing "Bye" and then get the related Id when click on Remove link and remove "Bye" div too.

var name1 = document.getElementById('name');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.one');
   result.innerHTML = this.value;
 });
 
  var name1 = document.getElementById('name');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.four');
   result.innerHTML = this.value;
 });

 var name1 = document.getElementById('city');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.two');
   result.innerHTML = this.value;
 });

 var name1 = document.getElementById('age');
 name1.addEventListener('input', function() {
   var result = document.querySelector('span.three');
   result.innerHTML = this.value;
 });

 $(document).ready(function() {
   var max_fields = 5; //maximum input boxes allowed
   var wrapper = $(".input_fields_wrap"); //Fields wrapper
   var add_button = $(".add_field_button"); //Add button ID
   var input_container = $(".container"); // holds all input containers

   var x = 1; //initlal text box count

   $(add_button).click(function(e) { //on add input button click
     e.preventDefault();
     if (x < max_fields) { //max input box allowed
       x++; //text box increment
       
       /* FIRST APPENDED ELEMENT */
       
       $(wrapper).append("<div class='container" + x + "' ><label>Name</label><input type='text'                            class= 'name' name='mytext[]'><a href='#' class='remove_field'>Remove</a><br><label>City</label><input type='text'                            class= 'city' name='mytext[]'><br><label>Age</label><input type='text'                            class= 'age' name='mytext[]'></div>");
       
       /* SECOND APPENDED ELEMENT */
       
       $('div.hello').append('<div id="container' + x + '" id = "' + x + '"   >Hello, <span class="name"></span> <span> ,from</span> <span class="city"></span> <span> Your age is:</span><span class="age"> </span><br><br></div>');
       
       /* THIRD APPENDED ELEMENT */
       
       $('div.hello').append('<div id="container_bye' + x + '" id = "' + x + '"   >Bye, <span class="name"></span><br><br></div>');

       $('input').on('input', function(e) {

         var parcontainer = $(this).parents('div').attr('class');


         spantoappend = $(this).attr('class');
         var val = $(this).val();
         var sel = "#" + parcontainer + '  .' + spantoappend;
         //  var sel = "#" + divtoappend + " span";
         $(sel).text('');
         $(sel).append(val);
       });

     }
   });

   $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
     e.preventDefault();
     
     /* THIS REMOVES SECOND APPENDED ELEMENT */ /* HOW DO I MAKE THIS REMOVE THIRD APPENDED ELEMENT TOO? */
     
     var rem = $(this).parents('div').attr('class');
     $('#' + rem).remove();
     $('#container_bye'+x).remove()
      /* THIS REMOVES FIRST APPENDED ELEMENT */ 
     var byElId = $(this).parents('div').attr('class').replace('container','')
     $(this).parent('div').remove();
     $("#container_bye"+byElId).remove()
      
     x--;

   });


 });
.block {
  display: block;
}

input {
  width: 50%;
  display: inline-block;
  margin: 4px;
  margin-left: 10px;
}

span.add,
span.remove {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}

label {
  display: inline-block;
  width: 40px;
  text-align: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input_fields_wrap">
  <button class="add_field_button">Add person</button>

  <br><br>
  <div class="container">
    <label>Name</label><input type="text" id="name" name="mytext[]"> <br>
    <label>City</label><input type="text" id="city" name="mytext[]"> <br>
    <label>Age</label><input type="text" id="age" name="mytext[]">
  </div>
</div>
<br>
<div class="hello">

  Hello, <span class="one"></span>, from <span class="two"></span>. Your age is: <span class="three"></span><br><br>

 Bye, <span class="four"></span><br><br>

</div>

<div class="bye">

   

</div>
Muhammad Usman
  • 10,039
  • 22
  • 39