0

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.

2 Answers 2

1

you want to continue adding headings, like this ?

$(document).ready(function() {
  var maxField = 100;
  var addButton = $('.btn_add_option');
  var wrapper = $('.wrap_add_input div.wrap_input');
  var fieldHTML = '';

  var x = 1;
  var counter = $(this).index();
  $(addButton).click(function() {

    if (x < maxField) {
      x++;
      var fieldHTML = '<div><h2>title'+x+'</h2><input type="text" id="" name="" style="width:135px;"><button type="button" class="btn_rmv_option">remove</button></div>';
      $(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>

Sign up to request clarification or add additional context in comments.

2 Comments

yes thank you. but when remove the element and then click add button the index value is getting wrong number
on element removal, decrease the value of 'x'
0

Try this code :

$(document).ready(function() {
var maxField = 100;
var addButton = $('.btn_add_option');
var wrapper = $('.wrap_add_input div.wrap_input');

var x = 1;
var counter = $(this).index();
$(addButton).click(function() {

  if (x < maxField) {
     x++;
    $(wrapper).append(setContent(x));
  }
 });
$(wrapper).on('click', '.btn_rmv_option', function(e) {
   e.preventDefault();
   $(this).parent('div').remove();
   x--;
 });
});

 function setContent(x){
       return '<div><h2>title'+x+'</h2><input type="text" id="" name="" style="width:135px;"><button type="button" class="btn_rmv_option">remove</button></div>';
   }

Just need to create dynamic content with value of index. Hope this helps.

1 Comment

Function setContent has dynamic content. I have updated content it had unexpected space. Kindly try again. @sam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.