PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Where to add the JQuery code

  1. gcollignon
    Member

    Hi I would like to use KevinĀ“s solutions to this post http://www.gravityhelp.com/forums/topic/limiting-the-amount-of-checkboxes-that-can-be-checked can someone please help me telling me how and where to add the code he provided?

    Thanks

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  2. David Peralty

    You'll be looking to add it to the header of your theme file. Making sure to change the input_1_15 li to target the input number of your checkbox that you want to control.

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  3. David's on the money as always.. this might help for future reference as well.

    http://www.gravityhelp.com/documentation/page/Where_Do_I_Put_This_Code%3F

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  4. gcollignon
    Member

    This is wonderful thanks, I have another question if you could please help me, In this same code can the limit # be variable. I mean can I create a drop down field with numbers and the number on that field is the limit #. How would that work?

    I was thinking something like this:

    <script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function($) {
    $.fn.limit = function(n) {
    var self = this;
    this.click(function(){ return (self.filter(":checked").length<=n); });
    }
    $("ul#input_1_15 li input:checkbox").limit("ul#input_1_15 li input:select");
    });
    </script>

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  5. gcollignon
    Member

    BTW Is there a post that talks about how to find the "input_1_15" where do I get this from?

    Thanks again

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  6. David Peralty

    I'm sorry, I'm not a JavaScript expert by any means. As for finding out the ID, if you view the source of your form, it should show up as a class that you can then grab and put into your script.

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  7. OK, so to do this, add your number field and give it a default value in the Advanced Tab of the field (you can then set this to be admin only for visibility). Find the ID of each field (on the number input and the dropdown ul by inspecting your form code).

    Your resulting jQuery would be this:

    jQuery.noConflict();
      jQuery(document).ready(function($) {
    	value = $("#input_85_1").val();
     	$.fn.limit = function(n) {
     	var self = this;
     	this.click(function(){ return (self.filter(":checked").length<=n); });
    	}
    	$("ul#input_85_2 li input:checkbox").limit(value);  				
    
      });

    See this Screenshot to show you how to find IDs. This is using Google Chrome Developer Tools, which is built-in to the browser. So in the jQuery above you would just replace the value line with the number field ID and then the second ul ID further down with the checkboxes ID.

    You can see a working example here

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink
  8. gcollignon
    Member

    Thanks Rob, I saw the working example but if I change the number to 4 I am still limited only to check 3 checkboxes. I was thinking on adding a dropdown with the number. I'd like to explain the reason why I am trying to do this, I am building an "order Pizza" the problem is that there are so many variations. Like first number of pizzas to order, second each pizza can be different then each one has a different price after that you need to select the number of ingredients (each extra ingredient also costs extra but it is a different price depending on the size of pizza) Now here where I ma using the limit checkboxes I am adding a dropdown asking for the number of ingredients and once they select the number then they will only be able to check those boxes. 4 ingredients will only allow them to select 4 checkboxes. Which takes me to the next problem every ingredient can be on the right half, the left half or the whole pizza. If one is selected as half then they have the right to add another half (it has been a nightmare for me honestly)

    Posted 12 years ago on Wednesday April 25, 2012 | Permalink