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.

Run Javascript on Specific Field

  1. poname
    Member

    I want to add auto suggest functionality to a custom post field titled "city/state". I have a script from google places library. I was wondering what type of hook I would use (gform enqueue script?) and where I would put it (functions.php?). Does gform enqueue script replace wp enqueue script when running a script for gravity forms?

    I'm kind of a newbie at this stuff but pretty intuitive, so could someone be kind enough to point me in the right direction?

    Thanks,
    Dan

    Posted 11 years ago on Friday February 8, 2013 | Permalink
  2. Yes, you can use gform_enqueue_scripts:
    http://www.gravityhelp.com/documentation/page/Gform_enqueue_scripts

    That will add the script to the page where the form is displayed. The gform_enqueue_scripts filter to enqueue your script will go in your theme's functions.php file.

    I'm not sure about replacing wp_enqueue_script. I think they work in tandem. For example, your script may rely on jQuery, and that would be enqueued by WordPress if it's a dependency for your Places script.

    Try putting something online and let us know if you have any trouble. Provide a link to the site where you implemented this and we'll take a look at it if you're having trouble.

    Posted 11 years ago on Friday February 8, 2013 | Permalink
  3. poname
    Member

    Thanks Chris! Much appreciated.

    I still have a question that is perhaps the most important one in regards to adding auto suggest: how do I localize this script to a specific, custom field? Would I do some thing like this: gform_enqueue_script_1_10? 1 being the form number and 10 being the specific field? I only want auto suggest functionality for city/state. It would be weird if the auto suggest location script fired on all fields.

    Thanks,
    Dan

    Posted 11 years ago on Friday February 8, 2013 | Permalink
  4. poname
    Member

    Here is what I have come up with so far. Letme preface this with the fact caveat that I'm a complete newbie with php so, have at it!

    <?php
    function gf_suggest() {
    wp-register_script('gf-suggest', get_stylesheet_directory(). 'http://maps.googleapis.com/maps/api/jslibraries=places&sensor=true');
    add_action('gform_enqueue_scripts_1_13', 'gf-suggest', 10, 2);
    	wp_enqueue_script('gf-suggest');
    }
    ?>
    Posted 11 years ago on Friday February 8, 2013 | Permalink
  5. This is incorrect:

    add_action('gform_enqueue_scripts_1_13'

    You can't use a field ID there.

    You can enqueue the script on the page or not. The enqueue function does not target any fields. Normally that is done when you call the script. The script needs to be in the page, loaded with gform_enqueue_scripts_$form_id. Then, there is normally another bit of JavaScript in the page, telling that script what fields it should be applied to.

    If you were to enqueue the script on the page, and nothing else, nothing at all will happen. Now that you are including it in the page, so you can use the functions, the next step is to call those functions, normally in additional JavaScript.

    Posted 11 years ago on Saturday February 9, 2013 | Permalink