Avatar of Yakup K
Yakup K
 asked on

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.
JavaScriptjQuery

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Yakup K

ASKER
Thanks a lot Julian for your great explanation.
Julian Hansen

You are welcome.
Yakup K

ASKER
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
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Yakup K

ASKER
Thanks very much. I really appreciate for your support 👍
Julian Hansen

You are welcome
Yakup K

ASKER
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 ?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Julian Hansen

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.