I am trying to set the value that is increase or decrease when click the Add button. The increasing or decreasing number is on H2 tag
This is what I tried so far.
$(document).ready(function() {
var maxField = 100;
var addButton = $('.btn_add_option');
var wrapper = $('.wrap_add_input div.wrap_input');
var fieldHTML = '<div><h2>title2</h2><input type="text" id="" name="" style="width:135px;"><button type="button" class="btn_rmv_option">remove</button></div>';
var x = 1;
var counter = $(this).index();
$(addButton).click(function() {
if (x < maxField) {
x++;
$(wrapper).append(fieldHTML);
}
});
$(wrapper).on('click', '.btn_rmv_option', function(e) {
e.preventDefault();
$(this).parent('div').remove();
x--;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap_add_input">
<button type="button" class="btn_add_option mar">add</button>
<div class="wrap_input">
<div>
<h2>title1</h2>
<input type="text" id="" name="" style="width:135px;">
</div>
</div>
What do I need to fix the code ? please help.