Show maximum 4 forms on user actions

I have got a form shown on the page at first. Then I would like to show another 3 forms , on the user actions.

jsfiddle

   <div id="form-0">

    form 0
    </div>

    <div id="form-1" class="add-form">
    <a class="remove-form">X</a>
    form 1
    </div>

    <div id="form-2" class="add-form">
    <a class="remove-form">X</a>
    form 2
    </div>

    <div id="form-3" class="add-form">
    <a class="remove-form">X</a>
    form 3
    </div>


<p class="request-text">Maximum of 4 per request</p>
<a href="#" class="add-another-form">+ Add another form </a>

Open in new window



$("a.remove-form").bind("click", function(e) {
    	    $(e.target).closest(".add-form").css("display", "none");
});

counter = 0;

   function checkCounter() {
          if (counter > 4)
          {
            $('.request-text').text('You have reached maximum 4 requests ');
            $(".request-text").css("color", "green");                
            $('.add-another-form').unbind('click');

            return false;
          }

          counter++;
          return true;
        }
        
        
        
// Add form

$(".add-another-form").click(function(){

     if (checkCounter()) {
    $(".add-another-form").bind("click", function(e) {
     $(e.target).closest(".add-form").css("display", "block");
        });
           counter++;
        }

});

Open in new window


.add-form {
  display: none;
}

Open in new window


I have tried a few different way, but I could not be able make it work as expected. Any helps would be really appreciated.
Yakup KAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
General Hint: format your code neatly - it is so much easier to see what is going on when it is nicely formatted - and on fiddle this is dead simple - you actually have to work at it to make the format go out of whack. Untidy code leads to mistakes and difficult debugging - don't do it!

You have not explained what the problem is.

Here are the problems I have seen.
1. Double increment
In checkCounter() you are incrementing counter
BUT
You are also incrementing it in the click handler

2. Double binding of event handler
You have this
$(".add-another-form").click(function(){

Open in new window

And inside that you have
 $(".add-another-form").bind("click", function(e) {

Open in new window

a) You are using bind() which has been deprecated in favour of on()
b) Why are you binding a click handler inside a click handler - makes no sense

3. You don't need to use e.target in the event handler - use this.
$(this).closest(".add-form").hide();

Open in new window


4. The following code
css("display", "block")

Open in new window

Can be replaced by
.show()

Open in new window


5. This is how you are trying to show a form
 $(e.target).closest(".add-form").css("display", "block");

Open in new window

That can't work because the clicked element is outside of the actual forms - the code above will work for a remove but not for an add. For an add you need to find the first hidden form and unhide it
$(".add-another-form").click(function(){
  if (checkCounter()) {
    $('.add-form:hidden:first').show();
    counter++;
  }
});

Open in new window

6. You are not decrementing your counter when you remove a form

7. Your check counter is checking for a value > 4 - but you are only providing for 3 forms to be added - should be >= 3

Here is the full JS code
var counter = 0;

$("a.remove-form").bind("click", function(e) {
  $(this).closest(".add-form").hide();
  counter--;
});

function checkCounter() {
  if (counter >= 3)
  {
    $('.request-text').text('You have reached maximum 4 requests ');
    $(".request-text").css("color", "green");                
    $('.add-another-form').unbind('click');

    return false;
  }
  return true;
}

// Add form

$(".add-another-form").click(function(){
  if (checkCounter()) {
    $('.add-form:hidden:first').show();
    counter++;
  }
});

Open in new window


Here is a working fiddle

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yakup KAuthor Commented:
Thanks a lot Julian for your great explanation.
Julian HansenCommented:
You are welcome.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Yakup KAuthor Commented:
Hi Julian,

if I add 3 forms and then removed them all. The add more form link does not show the forms again. Any ideas ?

Thanks
Julian HansenCommented:
Look at this code
function checkCounter() {
  if (counter >= 3)
  {
    $('.request-text').text('You have reached maximum 4 requests ');
    $(".request-text").css("color", "green");                
    $('.add-another-form').unbind('click'); // <<======= WHAT IS THIS DOING
    return false;
  }
  return true;
}

Open in new window

When you hit your max you are unbinding the click handler - so your click on the Add link is not going to work.
If you want to be able to add after hitting the limit and removing them then you have to remove that line.
Yakup KAuthor Commented:
Thanks very much. I really appreciate for your support 👍
Julian HansenCommented:
You are welcome
Yakup KAuthor Commented:
Hi Julian,

Thanks once again. Is there any ways, when user hide a form, I would like to add at the end of the all forms. This forms created with for loops on the Java.
<div id="form-0">
  form 0
</div>

<div id="form-1" class="add-form">
  <a class="remove-form">X</a>
  form 1
</div>

<div id="form-2" class="add-form">
  <a class="remove-form">X</a>
  form 2
</div>

<div id="form-3" class="add-form">
  <a class="remove-form">X</a>
  form 3
</div>

Open in new window


For example, When I hide the form 2, if user click add more I want this form to be added at the end of the all forms.
<div id="form-0">
  form 0
</div>

<div id="form-1" class="add-form">
  <a class="remove-form">X</a>
  form 1
</div>



<div id="form-3" class="add-form">
  <a class="remove-form">X</a>
  form 3
</div>

<div id="form-2" class="add-form">
  <a class="remove-form">X</a>
  form 2
</div>

Open in new window


Is there any ways I can try for this issue ?
Julian HansenCommented:
I would not solve it this way - I would dynamically add the form to the end of the container and then completely remove it when the user removes it. This allows for you to create as many forms as needed and they will always appear at the end.

Feel free to open another question if you need assistance with this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.