We help IT Professionals succeed at work.

Show maximum 4 forms on user actions

Yakup K
Yakup K asked
on
81 Views
Last Modified: 2019-03-18
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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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

Commented:
You are welcome.

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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

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 ?
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.