Home>Support>Using Javascript in the widget admin interface

Using Javascript in the widget admin interface

Hi there. I’m building a bunch of custom widgets based on the SiteOrigin_Widget class.

This far it’s been working really well but now I need to do something a little bit more complicated and I’m having some trouble figuring out how to solve this.

Basically what I want is to include a datetime-picker for one of my widgets. As far as I’ve seen in the documentation there is no premade element in the SO_Widget form builder.

So my idea was to add a regular textfield to my widget and then through javascript target that textfield and attach a javascript datetimepicker of some sort (not sure exactly which one yet).

I have been able to get a javascript running in my admin by enqueueing a script in the inherited initialize-function from the base-class, like so:

function initialize() {
    wp_enqueue_script( 
      'featured-product', 
      get_template_directory_uri() . '/widgets/featured-product/js/featured-product.js', 
      array( 'jquery' ), // dependencies
      '1.0', // version
      true // on footer
    );
  }

Though I have a feeling that this is not the best approach to do this, it should work.
My first question is how would you suggest to go on about enqueuing scripts to use in my widgets admin interface?

Secondly I wonder if there is a simple way of targeting an element within the widget admin interface. Can in some way get the element id from the instance of my Widget class and pass it to my javascript? Or should I rather modify the textelement form field and add some kind of target that way?

Thank you so much for an awesome plugin!

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 8 years, 7 months ago Alex S
    Hi, I Work Here

    Hi Jim,

    You can use the siteorigin_widgets_enqueue_admin_scripts hook to do this. If you haven’t seen it yet, the developer docs is a gold mine of information.

  2. 8 years, 6 months ago Jim Robert Nilsson

    Hi Alex

    I think I’ve been using the docs to much, don’t know how I could miss this, thanks!

  3. 8 years, 6 months ago Alex S
    Hi, I Work Here

    Hi Jim,

    Don’t worry, It happens. Is there anything else I can help you with today?

  4. 8 years, 6 months ago Jim Robert Nilsson

    No thank you, I have started another thread within another matter.

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More