Show maximum 4 forms on user actions

Yakup K
Yakup K used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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

Author

Commented:
Thanks a lot Julian for your great explanation.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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.

Author

Commented:
Thanks very much. I really appreciate for your support 👍
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome

Author

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 ?
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial