Web Analytics Made Easy -
StatCounter How to 'send options' to function? - CodingForum

Announcement

Collapse
No announcement yet.

How to 'send options' to function?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to 'send options' to function?

    I'm trying to make a jQuery slider, and I want the user (the person who installs it on their site) to be able to specify options easily. I know the Nivo slider does it from within the $(document).ready() event, but I can't figure out how to do it that way. I want it to be like this:

    Code:
    <script>
    $(document).ready(function() {
    myFunction({
    option1: 'value1',
    option2: 'value2',
    etc: 'etc'
    });
    });
    </script>
    But how do I recover this data so I can use it later?
    Originally posted by bullant
    Basically - Java is to Javascript as Ham is to Hampster.

  • #2
    Code:
    jQuery.fn.myPlugin.config=function(cfg){
      jQuery.fn.myPlugin.CF=cfg;  //or manually add props to default config one-at-a-time if use input is to be validated...
       return this;
    };

    then...

    Code:
    $.fn.myPlugin.config({
     speed:4,
     stop: false
    }).init();
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

    Comment


    • #3
      Originally posted by rnd me View Post
      Code:
      jQuery.fn.myPlugin.config=function(cfg){
        jQuery.fn.myPlugin.CF=cfg;  //or manually add props to default config one-at-a-time if use input is to be validated...
         return this;
      };

      then...

      Code:
      $.fn.myPlugin.config({
       speed:4,
       stop: false
      }).init();
      I don't exactly understand... can you elaborate a bit?
      Originally posted by bullant
      Basically - Java is to Javascript as Ham is to Hampster.

      Comment


      • #4
        I really need some help with this, anyone know how to do it?
        Originally posted by bullant
        Basically - Java is to Javascript as Ham is to Hampster.

        Comment


        • #5
          are you making a jQuery plug-in, or just a script that uses jQuery?

          you can accept options at
          run time as an optional argument,
          at load time as a config call or a static object being assigned to a config path,
          or pre-load time as an editable section of your distributable file - often seen at the top of script files.

          my example assumed a jQuery plug-in.
          it could call config() inline on a regular jQuery chain, continuing to actually invoke it with typical passed arguments after the next dot.

          where you store these settings is up to you, a settings path to an object is the simplest, but be sure to validate the input if this is going to be distributed...
          Last edited by rnd me; Sep 11, 2011, 06:34 PM.
          Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎