Link to home
Start Free TrialLog in
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.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yakup K
Yakup K

ASKER

Thanks a lot Julian for your great explanation.
You are welcome.
Avatar of 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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yakup K

ASKER

Thanks very much. I really appreciate for your support 👍
You are welcome
Avatar of 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 ?
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.