I need to add a dynamic text box and text area to one of my forms. I found this example that works great for adding a text box dynamically.
Javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10;
var wrapper = $(".input_fields_wrap");
var add_button = $(".add_field_button");
var x = 1;
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
$(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>');
}
});
$(wrapper).on("click",".remove_field", function(e){
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
HTML
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]"></div>
</div>
Result

I tried adding a text area
$(wrapper).append('<div><textarea name="desc[]"></textarea></div><a href="#" class="remove_field">Remove</a></div>');
to javascript above
and HTML
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]"></div>
<div><textarea name="desc[]"></textarea></div>
</div>
but it turns out to be wrong. How to add a text box with a text box?
Mistake
The maximum allowable limit is 10. Let's say I add 6 of these fields, and then decided to use 5 of them. If, I delete the last (sixth in this case), they are all deleted.
EDIT
Link to the above code https://jsfiddle.net/x6krv00u/
** I don't know much about javascripts. **
user5027402