Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    79
    Thanks
    3
    Thanked 4 Times in 4 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?
    Quote Originally Posted by bullant View Post
    Basically - Java is to Javascript as Ham is to Hampster.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    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();
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    79
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Quote 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?
    Quote Originally Posted by bullant View Post
    Basically - Java is to Javascript as Ham is to Hampster.

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    79
    Thanks
    3
    Thanked 4 Times in 4 Posts
    I really need some help with this, anyone know how to do it?
    Quote Originally Posted by bullant View Post
    Basically - Java is to Javascript as Ham is to Hampster.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    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; 09-12-2011 at 12:34 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •